简单的测验不适用于jQuery或JavaScript

时间:2017-02-16 09:25:30

标签: javascript jquery html

我正在进行一个简单的测验,以便在他提交“提交”时提供正确问题的答案。按钮。但是,它总是给予零。请帮忙。



$(document).ready(function(){
$('button').click(function(){
	var count = 0;
	if($('[name=q1]').val() == "Delhi"){count++;}
	if($('[name=q2]').val() == "Lotus"){count++;}
	alert(count);
});
});

<body> 

<div> 
<p>Question: What is capital of India?</p>

<input type="radio" name="q1" value="delhi"> Delhi
<input type="radio" name="q1" value="mumbai"> Mumbai
<input type="radio" name="q1" value="kolkata"> Kolkata
<input type="radio" name="q1" value="lucknow"> Lucknow
</div>

<div> 
<p>Question: What is the national flower of India?</p>

<input type="radio" name="q2" value="rose"> Rose
<input type="radio" name="q2" value="lotus"> Lotus
<input type="radio" name="q2" value="sunflower"> Sunflower
<input type="radio" name="q2" value="tulip"> Tulip
</div>

<button>submit</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

</body>
&#13;
&#13;
&#13;

  

总是给予&#39; 0&#39;但如果我选择&#39;德里&#39;和莲花&#39;它应该给出 2 的答案。

4 个答案:

答案 0 :(得分:7)

两件事:

  1. $('[name=q1]').val()会为您提供带有name="q1"第一个元素的值,而不是已检查的元素。为此,请添加:checked

  2. 您正在将"delhi""Delhi"进行比较;案件事宜。

  3. $(document).ready(function() {
      $('button').click(function() {
        var count = 0;
        if ($('[name=q1]:checked').val() == "delhi") {
          count++;
        }
        if ($('[name=q2]:checked').val() == "lotus") {
          count++;
        }
        alert(count);
      });
    });
    <div>
      <p>Question: What is capital of India?</p>
    
      <input type="radio" name="q1" value="delhi"> Delhi
      <input type="radio" name="q1" value="mumbai"> Mumbai
      <input type="radio" name="q1" value="kolkata"> Kolkata
      <input type="radio" name="q1" value="lucknow"> Lucknow
    </div>
    
    <div>
      <p>Question: What is the national flower of India?</p>
    
      <input type="radio" name="q2" value="rose"> Rose
      <input type="radio" name="q2" value="lotus"> Lotus
      <input type="radio" name="q2" value="sunflower"> Sunflower
      <input type="radio" name="q2" value="tulip"> Tulip
    </div>
    
    <button>submit</button>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

答案 1 :(得分:0)

你的Js代码有错误,你必须选择选中的输入,你的代码只选择第一个输入

&#13;
&#13;
$(document).ready(function(){
    $('button').click(function(){
	    var count = 0;
	    if($('input[name=q1]:checked').val() == "delhi"){count++;}
	    if($('input[name=q2]:checked').val() == "lotus"){count++;}
	    alert(count);
    });
});
&#13;
<body> 

    <div> 
        <p>Question: What is capital of India?</p>

        <input type="radio" name="q1" value="delhi"> Delhi
        <input type="radio" name="q1" value="mumbai"> Mumbai
        <input type="radio" name="q1" value="kolkata"> Kolkata
        <input type="radio" name="q1" value="lucknow"> Lucknow
    </div>

    <div> 
        <p>Question: What is the national flower of India?</p>

        <input type="radio" name="q2" value="rose"> Rose
        <input type="radio" name="q2" value="lotus"> Lotus
        <input type="radio" name="q2" value="sunflower"> Sunflower
        <input type="radio" name="q2" value="tulip"> Tulip
    </div>

    <button>submit</button>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js">                        </script>

</body>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

$(document).ready(function(){
$('button').click(function(){
	var count = 0;
if($('[name=q1]:checked').val() == "delhi"){count++;}
if($('[name=q2]:checked').val() == "lotus"){count++;}
	alert(count);
});
});
<body> 

<div> 
<p>Question: What is capital of India?</p>

<input type="radio" name="q1" value="delhi"> Delhi
<input type="radio" name="q1" value="mumbai"> Mumbai
<input type="radio" name="q1" value="kolkata"> Kolkata
<input type="radio" name="q1" value="lucknow"> Lucknow
</div>

<div> 
<p>Question: What is the national flower of India?</p>

<input type="radio" name="q2" value="rose"> Rose
<input type="radio" name="q2" value="lotus"> Lotus
<input type="radio" name="q2" value="sunflower"> Sunflower
<input type="radio" name="q2" value="tulip"> Tulip
</div>

<button>submit</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

</body>

答案 3 :(得分:0)

尝试使用大写字母(lotus =&gt; Lotus等)重命名您的值。