简体   繁体   English

关于使用jQuery设置HTML选择选项的问题

[英]Issue on Setting HTML Select Option Using jQuery

I am trying to change selected item from the list options dynamically at This Demo but it is not working. 我正在尝试在此演示中动态更改从列表选项中选择的项目,但是它不起作用。

<select id="selItems" class="selectpicker">
    <option>Select From List</option>
    <option>Mustard</option>
    <option>Ketchup</option>
    <option>Relish</option>
</select>
<p>
<button id="setdefault" type="submit" class="btn">Set Default</button>

<script>
 $("#setdefault").on("click", function () {
   $("#selItems option:eq(2)").attr("selected", "selected");
 });
</script>

can you please let me know what I am doing wrong? 您能告诉我我在做什么错吗?

Bootstrap Select has a method for doing this. Bootstrap Select具有执行此操作的方法。 You shouldn't be manually manipulating attributes because the element seen in the page isn't the original select element anyway. 您不应该手动操作属性,因为在页面中看到的元素无论如何都不是原始的select元素。

$("#setdefault").on("click", function () {
    $("#selItems").selectpicker('val', 'Ketchup');
});

Demo 演示版

http://silviomoreto.github.io/bootstrap-select/#methods http://silviomoreto.github.io/bootstrap-select/#methods

There are several reasons why your code is not working how you expect it to. 有几个原因导致您的代码无法按预期运行。

You are setting a click event on your select element, and not on the "set default" button. 您正在选择元素上设置点击事件,而不是在“设置默认值”按钮上设置。

Your actual select element is not visible on the page because it is being replaced by the jquery plugin you are using.. so even if you set the selected attribute of your select, you won't see it on the page. 您的实际select元素在页面上不可见,因为它正被您正在使用的jquery插件所取代..因此,即使您设置了select的selected属性,您也不会在页面上看到它。

You should refer to the documentation of that plugin to figure out how it is used. 您应该参考该插件的文档以了解其用法。

Try using prop instead of attr : 尝试使用prop而不是attr

  $("#setdefault").on("click", function () { $("#selItems option:eq(2)").prop("selected", true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selItems" class="selectpicker"> <option>Select From List</option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select> <p> <button id="setdefault" type="submit" class="btn">Set Default</button> 

Try switching 尝试切换

$("#setdefault").on("click",function(){
 $("#selItems option:eq(3)").attr("selected", "selected");
});

to

$("#setdefault").on("click",function(){
 $("#setdefault option:eq(3)").attr("selected", "selected");
});

Just use val() : 只需使用val()

 $("#setdefault").on("click", function() { $("#selItems").val("Mustard"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selItems" class="selectpicker"> <option>Select From List</option> <option>Mustard</option> <option>Ketchup</option> <option>Relish</option> </select> <p> <button id="setdefault" type="submit" class="btn">Set Default</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM