当项目从列表中选择时,选择一个单选按钮

时间:2016-11-30 22:48:42

标签: javascript jquery

我有以下代码。我在列表中有country字段,单选按钮中有currency字段。我想要的是,当我选择一个国家时,它会自动从单选按钮中选择货币。我有3种货币美国,欧元和美元英镑,对于欧盟,它将是欧元,对于英国,它将是英镑和欧元。其余的将是美元。

请帮帮我....

<!DOCTYPE html>
<html>
<head>
<title>Radio button and List Check</title></head>
    <body>
        <form class="form-horizontal" action="" method="post">
            <div class="form-group">
                <label class="col-sm-2 control-label">Country</label>
                <div class="col-sm-10">
                    <select class="form-control" id="country">
                        <option value="">Please select Country</option>
                        <option value="US">US</option>
                        <option value="EU">EU</option>
                        <option value="UK">UK</option>
                        <option value="China">China</option>
                        <option value="India">India</option>
                        <option value="Korea">Korea</option>
                        <option value="Asia">Asia</option>
                        <option value="Russia">Russia</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">Currency</label>
                <div class="col-sm-10">
                    <label class="radio-inline">
                        <input type="radio" name="optradio" >USD</label>
                    <label class="radio-inline">
                        <input type="radio" name="optradio">EUR</label>
                    <label class="radio-inline">
                        <input type="radio" name="optradio">GBP</label>
                </div>
            </div>
        </form>                 
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

试试这个

//This solution requires you to order currencies accordingly to countries

   var country = document.getElementById("country");
    var radios = document.querySelectorAll("[name='optradio']");
       country.onchange = function(){
           var si = this.selectedIndex;

           for(var i = 0; i < radios.length; i++){
              radios[i].checked = (si === i);
             }
      }