Javascript javascript中复选框的oncheck侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31705665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
oncheck listener for checkbox in javascript
提问by Gottfried Tetteh
I have code like this at the bottom of my form.
我的表单底部有这样的代码。
<p>
<input type="checkbox" id="agreed">
I agree to keep my Username and Password confidential and uphold
the integrity of this pharmacy
</p>
<input type="submit" id="submit" disabled class="formbutton button-primary" value="Go">
I want this a listener in JavaScript to be able to enable the submit button. I know it might be wrong or something but my JavaScript looks sort of like this
我希望这个 JavaScript 中的侦听器能够启用提交按钮。我知道这可能是错误的,但我的 JavaScript 看起来有点像这样
function track() {
if ( document.getElementById("agreed").checked==true ) {
document.getElementById("submit").removeAttribute("disabled");
} else {
document.getElementById("agreed").checked==false
}
};
回答by Lvkz
You can do this for the input:
您可以为输入执行此操作:
<input type='checkbox' onchange='handleChange(this);'>Checkbox
And this for enabling the button:
这是用于启用按钮的:
function handleChange(checkbox) {
if(checkbox.checked == true){
document.getElementById("submit").removeAttribute("disabled");
}else{
document.getElementById("submit").setAttribute("disabled", "disabled");
}
}
回答by Janty
Try Below for your requirement using jQuery.
尝试使用 jQuery 满足您的要求。
$('#checky').click(function(){
if($(this).attr('checked') == false){
$('#postme').attr("disabled","disabled");
}
else {
$('#postme').removeAttr('disabled');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<input type="checkbox" checked="checked" id="checky"><a href="#">I agree to keep my Username and Password confidential and uphold
the integrity of this pharmacy</a>
<br>
<input type="submit" id="postme" value="submit">
Using JavaScript, Please refer This
使用 JavaScript,请参考这里