所以我想禁用输入按钮功能,除非选中复选框。我不需要一个特定的复选框,但如果选中一个或多个复选框,则将启用删除按钮。我遇到的问题是,仅在选中第一个复选框时才启用删除按钮。例如,假设选中了第二个或第三个复选框,则删除按钮仍然处于禁用状态,除非也选中了第一个复选框。
有办法解决这个问题吗?
按钮:
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
复选框被回显,这是编码:
echo
"<tr>
<td><input type='checkbox' name='check' id='check'/><a href='' class='event-link'>$name</a>
</td><td>$date</td><td>$time</td><td>$venue</td>
</tr>";
我使用的脚本是:
var chkbox = $("#check"),
button = $("#del_event");
button.attr("disabled","disabled");
chkbox.change(function(){
if(this.checked){
button.removeAttr("disabled");
}else{
button.attr("disabled","disabled");
}
});
感谢任何帮助。
最佳答案
更改您的复选框生成的代码,如下所示:
echo
"<tr>
<td><input type='checkbox' name='check' class='check'/><a href='' class='event-link'>$name</a>
</td><td>$date</td><td>$time</td><td>$venue</td>
</tr>";
因为您想为每个复选框触发此事件 您需要在复选框中使用 class 而不是 id 。 在 jquery 中:
var chkbox = $(".check");
button = $("#del_event");
button.attr("disabled","disabled");
chkbox.change(function(){
if(this.checked){
button.removeAttr("disabled");
}else{
button.attr("disabled","disabled");
}
});
关于javascript - 除非选中复选框,否则禁用输入按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23150732/