gpt4 book ai didi

javascript - 复选框未定义行为

转载 作者:行者123 更新时间:2023-12-03 02:37:22 25 4
gpt4 key购买 nike

我对单选按钮和复选框有疑问。

请参阅下面的片段。这个想法如下:当用户按下“我想参与”按钮时,代码应该检查用户是否选择了该复选框,如果没有,则发出警报。

警报会在需要时引发,但是当我关闭它时,该复选框会被标记,即使之前没有。为什么会发生这种情况?

非常感谢!

<html>
<body>
<div id="consent"></div>
</body>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script type="text/javascript">


function consent() {
var HTMLtext = "<label> <input type='checkbox' name='cons' value='agree' id='consAgree' /> I am age 18 or older and understand the information above. <\label><br>";

HTMLtext += "<br><span id='submit_cons'><button type='button' onClick='javascript:evaluateConsent()'>I want to participate </button></span>";

document.getElementById('consent').innerHTML = HTMLtext;
}

function evaluateConsent() {
var isOK = 1;
if(document.getElementById("consAgree").checked == false) {
isOK = 0;
}
if(isOK == 0) {
alert('Please make sure you have confirmed all three conditions.\nIf you do not want ot participate, return the HIT');
}
else {
var HTMLtext = "Thank you! Now take a look at the example!";

document.getElementById('submit_cons').innerHTML = HTMLtext + "<hr>";
}
}
/*--MAIN--*/
function main(){
consent();
}

$(document).ready(function() {
//Ensure that form is not submitted upon pressing Enter key
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
//call function to run experiment
main();
});

</script>

</head>
</html>

最佳答案

您正在关闭 label标签如下:<\label> ,这是对此 </label> 的错误更改

function consent() {
var HTMLtext = "<label> <input type='checkbox' name='cons' value='agree' id='consAgree' /> I am age 18 or older and understand the information above. </label><br>";

HTMLtext += "<br><span id='submit_cons'><button type='button' onClick='javascript:evaluateConsent()'>I want to participate </button></span>";

document.getElementById('consent').innerHTML = HTMLtext;
}

function evaluateConsent() {
var isOK = 1;
if (document.getElementById("consAgree").checked == false) {
isOK = 0;
}
if (isOK == 0) {
alert('Please make sure you have confirmed all three conditions.\nIf you do not want ot participate, return the HIT');
} else {
var HTMLtext = "Thank you! Now take a look at the example!";

document.getElementById('submit_cons').innerHTML = HTMLtext + "<hr>";
}
}
/*--MAIN--*/
function main() {
consent();
}

$(document).ready(function() {
//Ensure that form is not submitted upon pressing Enter key
$(window).keydown(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
//call function to run experiment
main();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<div id="consent"></div>

看到了吗?现在没有被检查。

关于javascript - 复选框未定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48492956/

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