2

次のコードがあります。

 <li>1. question 1</li>
<li>
     <input type="radio" name="question1" id="sd1" value="1">Strongly Disagree
     <input type="radio" name="question1" id="d1" value="2">Disagree
     <input type="radio" name="question1" id="n1" value="3">Neither Agree Nor Disagree
    <input type="radio" name="question1" id="a1" value="4">Agree
     <input type="radio" name="question1" id="sa1" value="5">Strongly Agree
</li>
<br/><br/>

<li>2.  question 2 </li>
<li>
     <input type="radio" name="question2" id="sd2" value="1">Strongly Disagree
     <input type="radio" name="question2" id="d2" value="2">Disagree
     <input type="radio" name="question2" id="n2" value="3">Neither Agree Nor Disagree
    <input type="radio" name="question2" id="a2" value="4">Agree
     <input type="radio" name="question2" id="sa2" value="5">Strongly Agree
</li>
<br/><br/>

<li>3.  question 3</li>
<li>
     <input type="radio" name="question3" id="sd3" value="1">Strongly Disagree
     <input type="radio" name="question3" id="d3" value="2">Disagree
     <input type="radio" name="question3" id="n3" value="3">Neither Agree Nor Disagree
     <input type="radio" name="question3" id="a3" value="4">Agree
     <input type="radio" name="question3" id="sa3" value="5">Strongly Agree
</li>
<br/><br/>

  <input type="submit" value="Submit" name="Submit" id="Submit" />

そのような30の質問があります。私の要件は、ユーザーが 30 の質問すべてに答える必要があるということです。

ユーザーが質問に1つでも答えない場合にメッセージが表示されるように、JavaScript関数を作成するにはどうすればよいですか。編集:

私のjavascriptの問題はこれです:

   if ( (thisfrm.question3[0].checked == false) || (thisfrm.question3[1].checked == false) || (thisfrm.question3[2].checked == false) || (thisfrm.question3[3].checked == false) || (thisfrm.question3[4].checked == false))
{
    alert('Please answer question 1');
    return false;
}

上記のコードは、ループなしですべての質問に対して繰り返されます。つまり、クエリごとに書き込まれます。ただし、すべての質問に回答しても、質問 1 に回答してくださいと表示されます。

4

8 に答える 8

7

このメソッドは、jQuery を利用し、一連の回答でチェックされていないラジオ ボタンをチェックします。

HTML - 質問にクラスを追加<li>

<li>1. question 1</li>
<li class="option">
     <input type="radio" name="question1" id="sd1" value="1">Strongly Disagree
     <input type="radio" name="question1" id="d1" value="2">Disagree
     <input type="radio" name="question1" id="n1" value="3">Neither Agree Nor Disagree
     <input type="radio" name="question1" id="a1" value="4">Agree
     <input type="radio" name="question1" id="sa1" value="5">Strongly Agree
</li>

Javascript

// Delegate submit action
$(document).on('submit', 'form', function () {

    var validate = true;
    var unanswered = new Array();

    // Loop through available sets
    $('.option').each(function () {
        // Question text
        var question = $(this).prev();
        // Validate
        if (!$(this).find('input').is(':checked')) {
            // Didn't validate ... dispaly alert or do something
            unanswered.push(question.text());
            question.css('color', 'red'); // Highlight unanswered question
            validate = false;
        }
    });

    if (unanswered.length > 0) {
        msg = "Please answer the following questions:\n" + unanswered.join('\n'); 
        alert(msg);
    }
    return validate;
});

ここの例http://fiddle.jshell.net/6jNpQ/2/

于 2013-10-11T07:52:49.970 に答える
6

以下のコードは、初心者でもよりシンプルでクリーンです

if (!$("input[name='html_elements']:checked").val()) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

以下のフィドルデモをチェックしてください

http://jsfiddle.net/creators_guru/DBd79/ にアクセスしてください

于 2014-05-19T07:42:03.070 に答える
2

これをチェックして。各要素を検証する代わりに、次のようなものを使用できます

for(var i = 1 ; i <= 30 ; i++)
  {
      var radios = document.getElementsByName('question'+i);
      var checked = false;
      for (var j = 0, length = radios.length; j < length; j++) 
      {

         if (radios[j].checked) 
         {
          checked = true;
          break;
         }


       }
       if(!checked)
       {
         alert('Please answer question '+i);
         break;
       }
}
于 2013-10-11T07:40:50.560 に答える
1

JavaScriptのみを使用しているようです。JavaScript を簡単に記述できるように、jQuery を追加することをお勧めします。

とにかく、JavaScriptを使用するだけでこれを試すことができます:

var questions = 30;
var answer_checked = false;
var one_not_checked = false;
for (var i = 1; i <= questions; i++) {
    for (var j=0; j < document.getElementsByName('question'+i).length; j++) {
        answer_checked = answer_checked || (thisfrm['question'+i][j].checked == true);
    }
    one_not_checked = one_not_checked || !answer_checked;
    answer_checked = false;
}

if (one_not_checked) {
    // your message to the user as one answer is not checked
}
于 2013-10-11T07:51:58.537 に答える
0

これがあなたの質問に対する答えです。これも確認できます。

このコードを使用するだけで、コード用に記述しました。下記を参照してください

HTML コード

<form method="POST" action="/echo/html/?html=;delay=3;">
    <ul>
        <li class="question">1) question 1</li>
        <li class="answers">
            <input type="radio" name="question1" id="11" value="1" />Strongly Disagree
            <input type="radio" name="question1" id="12" value="2" />Disagree
            <input type="radio" name="question1" id="13" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question1" id="14" value="4" />Agree
            <input type="radio" name="question1" id="15" value="5" />Strongly Agree
        </li>

        <li class="question">2) question 2</li>
        <li class="answers">
            <input type="radio" name="question2" id="21" value="1" />Strongly Disagree
            <input type="radio" name="question2" id="22" value="2" />Disagree
            <input type="radio" name="question2" id="23" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question2" id="24" value="4" />Agree
            <input type="radio" name="question2" id="25" value="5" />Strongly Agree
        </li>

        <li class="question">3) question 3</li>
        <li class="answers">
            <input type="radio" name="question3" id="31" value="1" />Strongly Disagree
            <input type="radio" name="question3" id="32" value="2" />Disagree
            <input type="radio" name="question3" id="33" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question3" id="34" value="4" />Agree
            <input type="radio" name="question3" id="35" value="5" />Strongly Agree
        </li>
    </ul>
    <input type="submit" value="Submit" name="Submit" id="Submit" /> 
</form>

Javascript コード

$("form").submit(function () {
    var allChecked = true;
    $('li.answers').each(function () {
        if (!$(':radio', this).is(':checked')) {
            allChecked = false;
            return false;
        }
    });
    if (!allChecked) {
        alert('Please fill the form completely...');
        return false;
    } else {
        alert('You have filled the complete form.');
    }
});
于 2013-10-11T10:43:47.580 に答える
0

&&条件には論理演算子を使用する必要があります

if ( (thisfrm.question3[0].checked == false) && 
     (thisfrm.question3[1].checked == false) && 
     (thisfrm.question3[2].checked == false) && 
     (thisfrm.question3[3].checked == false) && 
     (thisfrm.question3[4].checked == false) )
于 2013-10-11T07:35:35.057 に答える
0

このようなもの?

if ($('input[type=radio]:checked').length <= 0) {
    alert("No radio checked")
}

それとも名前でやる?

if ($('input[name=question1]:checked').length <= 0) {
    alert("No radio checked")
}
于 2013-10-11T07:33:19.897 に答える