Javascript 如何在单击从下拉列表中选择选项时选中复选框:JS

Javascript 如何在单击从下拉列表中选择选项时选中复选框:JS,javascript,php,jquery,html,Javascript,Php,Jquery,Html,当我从选择下拉菜单中选择一个选项时,我想检查两个收音机框输入 我有一个选择框,如下所示: <div id="checkout-shipping-method-load"> <select id="shipping-method"> <option value="option1">1 twister ($14.99 + $6.99 S&H)</option> <option value="opti

当我从选择下拉菜单中选择一个选项时,我想检查两个收音机框输入

我有一个选择框,如下所示:

<div id="checkout-shipping-method-load">
    <select id="shipping-method">
        <option value="option1">1 twister ($14.99 + $6.99 S&H)</option>
        <option value="option2">1 twister ($14.99 + $6.99 S&H)</option>
    </select>
</div>
和收音机盒,如:

 <input type="radio" class="radio" checked="checked" id="s_method_flatrate2_flatrate2" value="flatrate2_flatrate2" name="shipping_method">
 <input type="radio" class="radio" id="s_method_flatrate_flatrate" value="flatrate_flatrate" name="shipping_method">
如何使所选选项与所选单选按钮相等


例如,如果选择了第一个选项,则选中第一个单选框,反之亦然。

使用jquery非常简单


您也可以添加条件来选择单选按钮

使用jquery非常简单

类似地,您可以添加条件以选择单选按钮,然后从单选按钮中删除选中的=选中的,然后重试

<div id="checkout-shipping-method-load">
    <select id="shipping-method">
          <option value="option1">1 twister ($14.99 + $6.99 S&H)</option>
          <option value="option2">1 twister ($14.99 + $6.99 S&H)</option>
    </select>
</div>


<input type="radio" class="radio" id="s_method_flatrate2_flatrate2" value="flatrate2_flatrate2" name="shipping_method">
<input type="radio" class="radio" id="s_method_flatrate_flatrate" value="flatrate_flatrate" name="shipping_method">

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

<script>

$('#shipping-method').change(function(){
    var option= $('#shipping-method').val();
    if(option == 'option1') {
        $('#s_method_flatrate2_flatrate2').attr('checked',true);
    }
    if(option == 'option2') {
        $('#s_method_flatrate_flatrate').attr('checked',true);
    }
});
</script>
Remove checked=从单选按钮中选中,然后重试

<div id="checkout-shipping-method-load">
    <select id="shipping-method">
          <option value="option1">1 twister ($14.99 + $6.99 S&H)</option>
          <option value="option2">1 twister ($14.99 + $6.99 S&H)</option>
    </select>
</div>


<input type="radio" class="radio" id="s_method_flatrate2_flatrate2" value="flatrate2_flatrate2" name="shipping_method">
<input type="radio" class="radio" id="s_method_flatrate_flatrate" value="flatrate_flatrate" name="shipping_method">

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

<script>

$('#shipping-method').change(function(){
    var option= $('#shipping-method').val();
    if(option == 'option1') {
        $('#s_method_flatrate2_flatrate2').attr('checked',true);
    }
    if(option == 'option2') {
        $('#s_method_flatrate_flatrate').attr('checked',true);
    }
});
</script>

我真的很喜欢San Krish的解决方案,但为了完整性起见,以下是我的简单回答:

我只想在选择框中检查change事件,然后设置单选按钮的相应索引

var select=document.getElementByIdshipping-method, rbs=document.getElementsByNameU方法; select.addEventListenerchange,函数{ rbs[select.selectedIndex].checked=true; }; 1台捻线机14.99美元+6.99美元S&H 1台捻线机14.99美元+6.99美元S&H
我真的很喜欢San Krish的解决方案,但为了完整性起见,以下是我的简单回答:

我只想在选择框中检查change事件,然后设置单选按钮的相应索引

var select=document.getElementByIdshipping-method, rbs=document.getElementsByNameU方法; select.addEventListenerchange,函数{ rbs[select.selectedIndex].checked=true; }; 1台捻线机14.99美元+6.99美元S&H 1台捻线机14.99美元+6.99美元S&H
可以使用Jquery设置单选按钮的checked属性,如下所示:

JQUERY:

注:

. 我还在下拉列表中添加了一个默认的选择选项,请参见 下面,如果默认设置为,则使用逻辑取消设置两个单选按钮 挑选出来的。这不是必需的,但我添加了它,以防对您有用。 HTML:


可以使用Jquery设置单选按钮的checked属性,如下所示:

JQUERY:

注:

. 我还在下拉列表中添加了一个默认的选择选项,请参见 下面,如果默认设置为,则使用逻辑取消设置两个单选按钮 挑选出来的。这不是必需的,但我添加了它,以防对您有用。 HTML:


基于相关元素位置的简单解决方案:

$('#shipping-method').change(function(){
   var optionSelected = $("option:selected", this);
   $(".radio:eq("+ optionSelected.index() +")").prop("checked", true);
});
尝试:

基于相关元素位置的简单解决方案:

$('#shipping-method').change(function(){
   var optionSelected = $("option:selected", this);
   $(".radio:eq("+ optionSelected.index() +")").prop("checked", true);
});
尝试:

嘿,这不是一个复选框。这是一个收音机钮扣。哦,对不起它的排错错误。radio@Mr.工程师嘿,这不是一个复选框。这是一个收音机钮扣。哦,对不起它的排错错误。radio@Mr.应使用Engineerprop代替attr应使用Prop代替attr
$('#shipping-method').change(function(){
   var optionSelected = $("option:selected", this);
   $(".radio:eq("+ optionSelected.index() +")").prop("checked", true);
});