gpt4 book ai didi

javascript - 检索图像中选定的单选按钮值

转载 作者:行者123 更新时间:2023-11-28 05:32:37 25 4
gpt4 key购买 nike

我想根据用户选择显示单选按钮值,但在警报时我没有获得这些值。我还想将单选按钮值显示为图像..就像当用户选择男性然后男性图片显示时

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id="test">
<h4>Gender</h4>
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<h4>Occupation</h4>
<input type="radio" name="occupation" value="PvtJob">Pvt. Job<br>
<input type="radio" name="occupation" value="Govt">Govt. Employee<br>
<h4>Loan Type</h4>
<input type="radio" name="loan" value="Personal">Personal Loan<br>
<input type="radio" name="loan" value="Health">Health Loan<br><br>
<button class="btn btn-primary pull-right place-order-button" name="submit" value="submit" onclick="return myFunction()" type="submit" >Finish</button>
</form>
</body>
<script type="text/javascript">
function myFunction() {
var radioButtons =["gender","occupation","loan"];

for( var x = 0; x < radioButtons.length; x ++) {
if ($('input[name = "'+radioButtons[x]+'"]:checked','#test').val()) {
alert("You checked " + $('input[name = "'+radioButtons[x]+'"]:checked','#test').attr('id'));
alert("Value is " + $('input[name = "'+radioButtons[x]+'"]:checked','#test').val());
}
}
}
</script>
</html>

最佳答案

在您的代码中包含 jquery,并且您的输入中提到了 snot 属性 id。您的代码中也没有诸如 test 之类的 id。您也不需要再次重复选择器,您可以简单地使用 $(this)

尝试下面的代码片段。

function myFunction() {
var radioButtons =["gender","occupation","loan"];

for( var x = 0; x < radioButtons.length; x ++) {
if ($('input[name = "'+radioButtons[x]+'"]:checked').val()) {
alert("Value is " + $(this).val());
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
<form id="test">
<h4>Gender</h4>
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<h4>Occupation</h4>
<input type="radio" name="occupation" value="PvtJob">Pvt. Job<br>
<input type="radio" name="occupation" value="Govt">Govt. Employee<br>
<h4>Loan Type</h4>
<input type="radio" name="loan" value="Personal">Personal Loan<br>
<input type="radio" name="loan" value="Health">Health Loan<br><br>
<button class="btn btn-primary pull-right place-order-button" name="submit" value="submit" onclick="return myFunction()" type="submit" >Finish</button>
</form>
</body>

关于javascript - 检索图像中选定的单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571354/

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