select第一个option选项选不中怎么回事_html/css_WEB-ITnose
程序员文章站
2022-05-17 21:52:43
...
$('#user_type').on('change', function(e) {
console.log(e.currentTarget.value);
var index = e.currentTarget.options.selectedIndex;
_eleTypeCode = e.currentTarget.value;
var text = e.currentTarget.options[index].text;
$("#user").val(text);
});
回复讨论(解决方案)
有没有人么啊
change事件是在选项改变时才会触发,页面加载后默认就是第一项被选中,这时你点了第一项,选项并没有改变,事件就不会被触发
可以在页面加载后让它没有选项被选中
$('#user_type')[0].selectedIndex = -1;
$('#user_type').on('change', function(e) {
console.log(e.currentTarget.value);
var index = e.currentTarget.options.selectedIndex;
_eleTypeCode = e.currentTarget.value;
var text = e.currentTarget.options[index].text;
$("#user").val(text);
});
实测可以选中任意一项, 但页面初始化时, 不会触发 onchange 事件, 所以也不会把 user 输入框的文本自动设置为第一项的值
3楼4楼说的对,加了$('#user_type')[0].selectedIndex = -1;果然可以选了
上一篇: PHP基础知识概览
下一篇: 没问号参数的地址是什么技术
推荐阅读
-
清空select标签中option选项的3种不同方式_html/css_WEB-ITnose
-
清空select标签中option选项的3种不同方式_html/css_WEB-ITnose
-
HTML学习笔记实例-创建一个注册页面select两个option出来结果却是四个选项,其中两个空白选项的原因?_html/css_WEB-ITnose
-
元素选不中是怎么回事。我疯了。第一次遇到这种事。_html/css_WEB-ITnose
-
HTML学习笔记实例-创建一个注册页面select两个option出来结果却是四个选项,其中两个空白选项的原因?_html/css_WEB-ITnose
-
select第一个option选项选不中怎么回事_html/css_WEB-ITnose
-
select第一个option选项选不中怎么回事_html/css_WEB-ITnose
-
怎么给控件select动态添加option选项_html/css_WEB-ITnose
-
元素选不中是怎么回事。我疯了。第一次遇到这种事。_html/css_WEB-ITnose
-
怎么给控件select动态添加option选项_html/css_WEB-ITnose