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

时间:2016-09-19 10:50:51

标签: javascript jquery html radio-button radio

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

<!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>

3 个答案:

答案 0 :(得分:0)

在代码中包含jquery,输入中提到了snot属性id。您的代码中也不存在诸如test之类的ID。 您也不需要再次重复选择器,您可以简单地使用{ "users": { $uid: { "username": "callam", ... "role": "member" } }, "usernames": { "callam": $uid } }

请尝试以下代码段。

$(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());
        }
    }
}

答案 1 :(得分:0)

由于您使用的是Jquery功能,因此需要在标头中添加Jquery库。请添加

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

进入标题部分并尝试

答案 2 :(得分:0)

您可以使用length对象的proprty来检查所选元素。 在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>