Dropdown option validation for pre-filled select f

2019-07-12 19:52发布

I have this JQuery piece to prevent selection of dropdown options that are already selected in another field.

    var $coll = $( 'select[name$="service"]' ).on( 'change', function () {
        $coll.each(function () {
              var val = this.value;
              if ( val === 'original' ) return;
              $coll.not( this ).children( '[value="' + val + '"]' ).prop( 'disabled', true );
        });
    });

The problem is that this only works when you start selection from scratch (empty form). When I have a form that has fields with pre-selected options, the function only kicks in after the second attempt.

Do you know how I can change this to check for pre-filled fields as well?

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-12 20:37

All I had to do was to .trigger("change") after document ready, so that the validation begins before the first selection.

查看更多
登录 后发表回答