如何使用JQuery通过选项列表索引号改变JQuery-select2下拉列表的选择值
How to change selected value of JQuery-select2 dropdown by option list Index number using JQuery
我使用下拉框,这是使用jquery -select版本3.5实现的。
我想用JQuery设置从列表中选择的第二个项目,如$("#fieldId").prop("selectedIndex",1)
。
有可能吗?
Thanks in advance
$('#fieldId').select2("val",$('#fieldId option:eq(1)').val());
/*By using eq(), the index number should be calculated from 0*/
或
$('#fieldId').select2("val",$('#fieldId option:nth-child(2)').val());
/*By using nth-child(), the index number should be calculated from 1*/
可能有用
如果你指的是select
下拉菜单,那么这应该可以解决你的问题
<select>
<option>1st item</option>
<option selected>2nd item</option>
<option>3rd item</option>
</select>
编辑:你可以这样做
$('select option:nth-child(2)').attr('selected', true);
$('#selectId').find('option').each(function(index,element){
if(element.value.toLowerCase().trim() == 'VAL_TO_MATCH'){
$("#selectId").prop('selectedIndex', index).change();
}
});
相关文章:
- Ajax模式下的Jquery Select2 V4在IE9中不起作用
- jquery-select2显示默认选择的第一个值
- Jquery Select2 Ajax+Tags
- jQuery select2 动态选项
- 无法使用 jQuery .select2 选择项目
- JQuery Select2子项未显示为选中
- Jquery Select2.js在迁移后不起作用
- jQuery Select2数据属性
- 如何设置jQuery Select2组合框的容器样式
- JQuery-Select2 -操作发送的http-post数据以发送文本而不是id
- jQuery select2与多个选择标签
- Jquery select2 -获取多个选择的单个id
- Jquery select2标签的默认值
- jQuery select2 -数据属性与ajax
- 在jquery-select2中输入不在选择列表中的值
- jQuery Select2远程数据加载:显示选项而不是占位符
- Jquery Select2:不能设置外部变量
- jQuery select2不能在自定义模板中添加click事件
- jquery select2 -通过AJAX格式化结果
- jQuery select2 -当检测到某个值时更改所选值