触发更改功能并在jquery中设置select的值?

时间:2015-06-15 08:43:18

标签: javascript jquery ruby-on-rails

您好我已经给了代码

$('#filled-in-box2').on('click', function () {
    if ($('#filled-in-box2').is(':checked')) {
        $('select#employee_permanent_address_attributes_country').val($('select#employee_contact_attributes_country').val()).change();
      $('select#permanent_address_state_code').val($('select#contact_state_code').val());
    }
});

所以我想要在更改功能之后或当更改功能触发时,只有 permanent_address_state_code 值将设置为与 contact_state_code值相同。 我也尝试过这个代码,但它不能正常工作

$('#filled-in-box2').on('click', function () {
    if ($('#filled-in-box2').is(':checked')) {
        $('select#employee_permanent_address_attributes_country').val($('select#employee_contact_attributes_country').val()).trigger('change',function(){
            $('select#permanent_address_state_code').val($('select#contact_state_code').val());
        });

    }
  });

我点击复选框时附加快照它没有填充状态 enter image description here

请指导我如何解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

$('#filled-in-box2').on('click', function () {
   if ($('#filled-in-box2').is(':checked')) {                           $('select#employee_permanent_address_attributes_country').val($('select#employee_contact_attributes_country').val());
    $('select#permanent_address_state_code').val($('select#contact_state_code').val()); 
 }
 });

当您单击复选框时,仅此一项就可以满足您更改永久州和国家/地区的要求。