我正在练习JavaScript,如果单击表单中的任何单选按钮,我正在尝试使按钮可单击。但我的$('形式:输入')。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>
事情是,我做了一个小提琴(https://jsfiddle.net/3wLmxmu6/4/)并且正在研究小提琴。但是在我的电脑上却不是。在过去的两个小时里,我一直在为此而奋斗,无法找到任何理由。 我的浏览器控制台在chrome和firefox中都没有显示错误/警告。 谁能告诉我这里发生了什么,我该如何解决?非常感谢你。
答案 0 :(得分:1)
你获得挫折的原因是因为你的问题缺乏研究。
如何找到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