检索图像中选定的单选按钮值
Posted
技术标签:
【中文标题】检索图像中选定的单选按钮值【英文标题】:retrieve selected radio button value in a image 【发布时间】:2017-01-27 00:51:53 【问题描述】:我想根据用户选择显示单选按钮值,但在提醒时我没有得到值。 我还想将单选按钮值显示为图像..就像当用户选择男性然后男性图片显示时
<!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库的引用 【参考方案1】:在您的代码中包含 jquery,并且您的输入中提到了 snot 属性 id。您的代码中也没有诸如 test 之类的 id。
你也不需要再重复选择器,你可以简单地使用$(this)
试试下面的 sn-p。
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>
【讨论】:
【参考方案2】:由于您使用的是 Jquery 功能,因此您需要在标题中添加 Jquery 库。请添加
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
进入标题部分并尝试
【讨论】:
【参考方案3】:您可以使用对象的length
属性检查所选元素。还 在您的 HTML 页面中添加 jquery 库路径。
function myFunction()
var radioButtons =["gender","occupation","loan"];
for( var x = 0; x < radioButtons.length; x ++)
selected=$('input[name = "'+radioButtons[x]+'"]:checked','#test');
if (selected.length>0)
alert(radioButtons[x] + ":: Value is " + selected.val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form id="test">
<h4>Gender</h4>
<input type="radio" name="gender" value="male" id=""> 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>
【讨论】:
你走在正确的道路上.. 但我想将选定的值显示为像选择男性这样的 div 中的图像然后它显示男性图片而不是男性.. 提前致谢。以上是关于检索图像中选定的单选按钮值的主要内容,如果未能解决你的问题,请参考以下文章