javascript - ajax接收到值,那个值和select的option的其中一个值一样的话就选中,代码怎么写
天蓬老师
天蓬老师 2017-04-11 10:58:31
[JavaScript讨论组]

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
ringa_lee

每个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 );
                }
            }
PHP中文网

jQuery的话不需要遍历,直接

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

就能设置。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号