通过单击复选框在两个下拉列表中设置相同的值

时间:2015-08-28 06:32:41

标签: javascript jquery html ruby-on-rails

我正在为国家DropDown使用Carmen gem。我在两个地方使用卡门。我想通过单击复选框将这些下拉列表设置为与所选值相等。下拉列表的Html代码: -

 <select id="contact_attributes" name="dealer[contact_attributes][country]" class="initialized">
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="IN" selected="selected>India</option>
</select>

 <input type="checkbox" id="SameAsCurrentAddress" class="filled-in">

 <select id="permanent_address" name="dealer[permanent_address_attributes][country]" class="initialized">
 <option value="AF">Afghanistan</option>
 <option value="AL">Albania</option>
 <option value="IN">India</option>
 <option value="US" selected="selected">United States</option
 <option value="AD">Andorra</option>
 <option value="AO">Angola</option>
 </select>

我的代码在这里:

$('#SameAsCurrentAddress').on('click', function () {
    if ($('#SameAsCurrentAddress').is(':checked')) {
      $('select#permanent_address').val($('select#contact_attributes').val());
});

但是我无法设置值eqaul。如何设定平等?请帮帮我。

1 个答案:

答案 0 :(得分:0)

你有很多语法错误。请使用像Eclipse这样的好IDE来为您捕获这些错误。这有效:

&#13;
&#13;
 <select id="contact_attributes" name="dealer[contact_attributes][country]" class="initialized">
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="IN" selected="selected">India</option>
</select>

 <input type="checkbox" id="SameAsCurrentAddress" class="filled-in">

 <select id="permanent_address" name="dealer[permanent_address_attributes][country]" class="initialized">
 <option value="AF">Afghanistan</option>
 <option value="AL">Albania</option>
 <option value="DZ">Algeria</option>
     <option value="US" selected="selected">United States</option>
 <option value="AD">Andorra</option>
 <option value="AO">Angola</option>
 </select>
&#13;
&#13;
&#13;

$('#SameAsCurrentAddress').click(function () {
    if ($('#SameAsCurrentAddress').is(':checked')) {
      $('#permanent_address').val($('#contact_attributes').val());
    }
});