2017-08-24 34 views
-3

我正在练习JavaScript,并且当点击表单中的任何单选按钮时,我正在尝试使按钮可点击。但是我的$('form:input')。change()事件永远不会被触发。 这里是HTML

<!DOCTYPE html> 
<html> 
<head> 
<title>blah</title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script> 
<script> 
$('form :input').change(function() { 
    alert("changed"); 
    if($("input[type=radio]:checked").length === 0){ 
     $('#voteButton').prop('disabled', true); 
    }else{ 
     $('#voteButton').prop('disabled', false); 
    } 
}) 
$(document).ready(function() { 
     if($("input[type=radio]:checked").length === 0){ 
      $('#voteButton').prop('disabled', true); 
     } 
    }); 
</script> 
</head> 
<body> 
<form>  
    <input type="radio" name="choice" id="choice1" value="1"> 
    <label for="choice1">Just hacking again</label><br> 
    <input type="radio" name="choice" id="choice2" value="2"> 
    <label for="choice2">Nothing</label><br> 
    <input type="submit" value="vote" name="voteButton" id="voteButton"> 
</form> 
</body> 
</html> 

的事情是,我做了一个js小提琴(https://jsfiddle.net/3wLmxmu6/4/)和它的工作的小提琴。但是在我的电脑上,事实并非如此。在过去的2个小时里,我一直在为这件事而头痛,并且找不到任何理由。 我的浏览器控制台在chrome和firefox中都没有显示任何错误/警告。 任何人都可以告诉我这里发生了什么,如何解决它?非常感谢你。

+2

'$('形式:输入')...'文档就绪处理程序内部 – Satpal

+1

传入答案,而不是投票结束在3 ... 2 .. 1 ... –

+0

@RoryMcCrossan错字,你还在计数? – Satpal

回答

1

你正在得到downvotes的原因是因为你的问题缺乏研究。

如何jQuery的变化的作品,可以发现here

至于你的问题:

<form>  
     <input type="radio" name="choice" id="choice1" value="1"> 
     <label for="choice1">Radio 1</label><br> 
     <input type="radio" name="choice" id="choice2" value="2"> 
     <label for="choice2">Radio 2</label><br> 
     <input type="submit" value="vote" name="voteButton" id="voteButton" disabled = 'true'> 
    </form> 

脚本:

$(document).ready(function() { 
     $('form :input').change(function() { 
      alert("changed"); 
      if($("input[type=radio]:checked").length === 0){ 
       $('#voteButton').prop('disabled', true); 
      }else{ 
       $('#voteButton').prop('disabled', false); 
      } 
     }) 
    }); 

更新您的提琴here