gpt4 book ai didi

jquery - jquery 中的复选框验证

转载 作者:行者123 更新时间:2023-12-01 06:13:01 25 4
gpt4 key购买 nike

我需要表单上选择复选框的帮助。我想在未选中表单复选框时显示警报(仅)。我的代码:

<form action="index.php?page=contact&amp;send=ok" method="POST" id="form">
[...]
<label for="message">Checkbox:</label><input type="checkbox" id="checkbox" name="checkbox">
<input type="submit" value="Send" onclick="testcheck()" />
</form>

<script type="text/javascript">
function testcheck()
{
if (jQuery("#checkbox").prop("checked"))
alert("first button checked");
else
alert("none checked");
}
</script>

最佳答案

这段代码应该可以工作:

function testcheck()
{
if (!jQuery("#checkbox").is(":checked")) {
alert("none checked");
return false;
}
return true;
}

这是 HTML 代码:

<input type="submit" value="Send" onclick="return testcheck()" />

return false 将停止执行默认操作(表单提交)

关于jquery - jquery 中的复选框验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13239973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com