使用JQuery预填充选择字段的下拉选项验证 [英] Dropdown option validation for pre-filled select fields with JQuery

查看:85
本文介绍了使用JQuery预填充选择字段的下拉选项验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  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);
});
});

问题在于,这仅适用于从头开始选择(空格式)的情况。
当我有一个带有预选选项的表单时,该功能只会在第二次尝试后进入。



你知道我该如何改变它来检查预填充字段吗?

方案

在文档准备好之后,我只需要执行 .trigger(change),以便验证在第一次选择之前开始。 / p>

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?

解决方案

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

这篇关于使用JQuery预填充选择字段的下拉选项验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆