ajax接收到值,那个值和select的option的其中一个值一样的话就选中,代码怎么写

ajax接收到值,那个值和select的option的其中一个值一样的话就选中,代码怎么写

阅读 4.1k
3 个回答

每个option设置个value属性,用jquery的话:

var hasOpt=$("#selectID").find("option[value*='ajax传的值']");
if(hasOpt){
    $("#selectID").val("ajax传的值");
}

for ( ; i < max; i++ ) {

                option = options[ i ];

                // Support: IE <=9 only
                // IE8-9 doesn't update selected after form reset (#2551)
                if ( ( option.selected || i === index ) &&

                        // Don't return options that are disabled or in a disabled optgroup
                        !option.disabled &&
                        ( !option.parentNode.disabled ||
                            !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {

                    // Get the specific value for the option
                    value = jQuery( option ).val();

                    // We don't need an array for one selects
                    if ( one ) {
                        return value;
                    }

                    // Multi-Selects return an array
                    values.push( value );
                }
            }

jQuery的话不需要遍历,直接

$("#selectid").val(value)

就能设置。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题