代码之家  ›  专栏  ›  技术社区  ›  user2828442

单选按钮总是返回“是”值

  •  1
  • user2828442  · 技术社区  · 6 年前

    使用简单的单选按钮,如果我选择 yes no 选项。

    下面是我的代码:

    $('input:radio[name="selectlinktype"]').change(function() {
      if ($('input:radio[name="selectlinktype"]').val() == 'Yes') {
        alert("yes");
        var dropdownval = $("#dropDownsub").val();
        target_element.removeAttr('href').attr('action', dropdownval);
      }
    
      if ($('input:radio[name="selectlinktype"]').val() == 'No') {
        alert("no");
        var dropdownval = $("#dropDownsub").val();
        // target_element.removeAttr('href');
        target_element.removeAttr('action').attr('href', $("#edit-link-url .url").val());
        $(event.target).attr("href", $("#edit-link-url .url").val());
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link" name="selectlinktype" value="Yes" /> Yes
    <input type="radio" id="href-link" name="selectlinktype" value="No" /> No

    选择是或否,它将始终进入内部 条件, 条件永远不会被激发。我哪里出错了?

    5 回复  |  直到 5 年前
        1
  •  1
  •   Justinas    6 年前

    您正在选择名为的第一个元素 selectlinktype 所以总是这样 yes . 添加 :checked 以选择实际选定的元素。

    $('input:radio[name="selectlinktype"]').change(function(){
      if($('input:radio[name="selectlinktype"]:checked').val() == 'Yes'){
         alert("yes");
         var dropdownval = $("#dropDownsub").val();
         target_element.removeAttr('href').attr('action', dropdownval);
      }
      
      if($('input:radio[name="selectlinktype"]:checked').val() == 'No'){
       alert("no");
        var dropdownval = $("#dropDownsub").val();
       // target_element.removeAttr('href');
        target_element.removeAttr('action').attr('href',$("#edit-link-url .url").val() );
        $(event.target).attr("href" , $("#edit-link-url .url").val()); 
     }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link"  name="selectlinktype" value="Yes" /> Yes
      <input type="radio" id="href-link"  name="selectlinktype" value="No" /> No
        2
  •  3
  •   Amadan    6 年前

    input:radio[name="selectlinktype"] .val() 检查 value 第一个,碰巧是 "yes" value="yes" ).

    input:radio[name="selectlinktype"]:checked

    this event.target .

        3
  •  1
  •   Bhushan Kawadkar    6 年前

    您正在检查名为selectlinktype的单选按钮的值,但需要检查单击并选中的单选按钮

    if else if 如果 $(this) 对于目标元素,请参见下面的代码

     $('input:radio[name="selectlinktype"]').change(function(){
      var value = $(this).val();
      if(value == 'Yes'){
         alert("yes");
         var dropdownval = $("#dropDownsub").val();
         $(this).removeAttr('href').attr('action', dropdownval);
      } else if(value == 'No'){
       alert("no");
        var dropdownval = $("#dropDownsub").val();
       // target_element.removeAttr('href');
        $(this).removeAttr('action').attr('href',$("#edit-link-url .url").val() );
        $(this).attr("href" , $("#edit-link-url .url").val()); 
     }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link"  name="selectlinktype" value="Yes" /> Yes
    <input type="radio" id="href-link"  name="selectlinktype" value="No" /> No
        4
  •  1
  •   Mamun    6 年前

    您当前的选择器与两个单选按钮都匹配。任意使用 :checked 作为选择器的一部分或 this

    $('input:radio[name="selectlinktype"]').change(function(e){
      console.clear();
      console.log(e.target);
      if($(this).val() == 'Yes'){
         alert("yes");
         //var dropdownval = $("#dropDownsub").val();
         //target_element.removeAttr('href').attr('action', dropdownval);
      }  
      else if($(this).val() == 'No'){
       alert("no");
        //var dropdownval = $("#dropDownsub").val();
        //target_element.removeAttr('href');
        //target_element.removeAttr('action').attr('href',$("#edit-link-url .url").val() );
        //$(event.target).attr("href" , $("#edit-link-url .url").val()); 
     }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link"  name="selectlinktype" value="Yes" /> Yes
    <input type="radio" id="href-link"  name="selectlinktype" value="No" /> No
        5
  •  0
  •   David    6 年前

    您可以通过单击事件来管理它

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link"  name="radio_name" value="Yes" /> Yes
    <input type="radio" id="href-link"  name="radio_name" value="No" /> No
    
    <script>
    $('input:radio[name="radio_name"]').click(function(){
      var radio_val = $(this).val()
      if(radio_val == 'Yes'){
         alert("yes");
      }  
      else if(radio_val == 'No'){
       alert("no");
     }
    });
    </script>
    
        6
  •  0
  •   Rahul Koshti    5 年前

     $('input:radio[name="selectlinktype"]').change(function(){
      var value = $(this).val();
      if(value == 'Yes'){
         alert("yes");
         var dropdownval = $("#dropDownsub").val();
         $(this).removeAttr('href').attr('action', dropdownval);
      } else if(value == 'No'){
       alert("no");
        var dropdownval = $("#dropDownsub").val();
       // target_element.removeAttr('href');
        $(this).removeAttr('action').attr('href',$("#edit-link-url .url").val() );
        $(this).attr("href" , $("#edit-link-url .url").val()); 
     }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="radio" id="int-link"  name="selectlinktype" value="Yes" /> Yes
    <input type="radio" id="href-link"  name="selectlinktype" value="No" /> No