gpt4 book ai didi

Javascript:检查表单时显示警报?

转载 作者:行者123 更新时间:2023-12-02 16:05:49 25 4
gpt4 key购买 nike

我创建了一个简单的 Web 应用程序,它有 2 个表单选择,当用户从每个表单中进行选择时,我希望显示一个警报,向他们展示他们所做的选择。

我在下面尝试了此操作,但是当检查两种表单时,警报显示。我缺少什么?注意看评论:

 //BELOW IS NOT BEING DISPLAYED BUT SHOULD BE

当前代码:

<!DOCTYPE html>
<html>
<body>

<h1>Calculator</h1>

<p>Select the importance of this:</p>

<form method="get">
<input type="checkbox" name="Severity" value="negligible"> Negligible<br>
<input type="checkbox" name="Severity" value="minor"> Minor<br>
</form>

<p>Select the Probability of this:</p>

<form method="get">
<input type="checkbox" name="Probability" value="improbable"> Improbable<br>
<input type="checkbox" name="Probability" value="remote"> Remote<br>
<input type="checkbox" name="Probability" value="occasional"> Occasional<br>

<button onclick= "analyseThis()"> Analyse this </button> <br>



<script>

function analyseThis(){

var severities = document.getElementsByName('Severity');
var probs = document.getElementsByName('Probability');

var severity = undefined;
for(var i = 0; i < ages.length; i++)
{
if(severities[i].checked)
{
age = severities[i].value;

}
}

var probability = undefined;
for(var i = 0; i < probs.length; i++)
{
if(probs[i].checked)
{
probability = probs[i].value;

}
}

//BELOW IS NOT BEING DISPLAYED BUT SHOULD BE
alert("Severity is: " + age + "Probability is: " + probability);

}

</script>


</body>
</html>

最佳答案

我会使用JQuery并使用按钮的点击功能来提交。

这是一个例子:

$(document).ready(function () {
$("{form id/class button id/class OR button id/class}").click(function(e) {
e.preventDefault();
//perform validation

//if everything is good...
$("{form id/class}").submit();
});
});

关于Javascript:检查表单时显示警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30758576/

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