简体   繁体   English

Jquery .val() 函数检查单选按钮的值

[英]Jquery .val() function to check the value of radio buttons

Below is my logic to check the value of each of the radio buttons.下面是我检查每个单选按钮值的逻辑。 I am using a part of their id's to get hold of the radio buttons.我正在使用他们 ID 的一部分来获取单选按钮。 However, my code is always returning a value based on the value of my first radio button.但是,我的代码总是根据我的第一个单选按钮的值返回一个值。 I want it to return the value of each radio button.我希望它返回每个单选按钮的值。 For example if radio button clicked is yes, then value returned should be 1. Else, 0. Any body who can update my code please.例如,如果单击的单选按钮为是,则返回的值应为 1。否则为 0。请任何可以更新我的代码的机构。

$('.YesNoRadio').each(function() {

    if ($('[id*="YesNo_RadioButtonList_"] input[type="radio"]:checked').val() == 1) {
        //$('[id*="AddAttachment"]').trigger('click');
        $('[id="upload"]').click();
    }

});

Html:网址:

//For yes radio button
<input id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="1">
//For no
<input id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="0">

Your selector is incorrect, Remove space from selector.您的选择器不正确,从选择器中删除空格。 When you use " " it indicates you are targeting child elements ie Descendant Selector (“ancestor descendant”)当您使用" "它表示您的目标是子元素,即后代选择器(“祖先后代”)

if($('[id*="YesNo_RadioButtonList_"]:radio:checked').val() == 1)
   $('[id="upload"]').click();

And, You don't need .each()而且,你不需要.each()

//For yes radio button
<input class="YesNoRadio" id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="1">
//For no
<input class="YesNoRadio" id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="0">

<script type="text/javascript">
    $('.YesNoRadio').each(function() {
        if ($(this).is(':checked')) {
            alert($(this).val());
            //code for yes radio button
            $('[id="upload"]').click();
        }else{
            alert($(this).val());
            //code for no radio button
        }
    });
</script>

A method that worked for me was setting the name in the html input tag the same for each radio button is your list, which you did:一种对我有用的方法是在 html 输入标签中为每个单选按钮设置相同的名称是你的列表,你这样做了:

<input id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="1">
<input id="_YesNo_RadioButtonList_0" type="radio" name="YesNo_RadioButtonList" value="0">

then I would do this is my JS with JQuery.然后我会用 JQuery 来做我的 JS。

var radioButtonValue = $("input[name='YesNo_RadioButtonList']:checked").val();

This should give you the value of your radio button.这应该为您提供单选按钮的价值。 The value, in your case, is preset in you input tag.在您的情况下,该值已在您的输入标签中预设。

Try this code试试这个代码

$('.YesNoRadio').each(function() {
    if ($('[id*="YesNo_RadioButtonList_"] input[type="radio"]:checked').val() == 1) {
       //$('[id*="AddAttachment"]').trigger('click');
       console.log($(this).val());
       $('[id="upload"]').click();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM