[英]Select option is not updated 'selected' property in HTML web page
Please, explain how can I change 'selected' property of option? 请解释我如何更改选项的“选定”属性? Eg:
例如:
<select id="lang_select">
<option value="en" selected="selected">english</option>
<option value="ar">العربية</option>
<option value="az">azərbaycanlı</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="cs">český</option>
<!-- some data cut -->
</select>
So, if I change the drop down list value nothing is changed in html-data. 因此,如果我更改下拉列表值,则html-data中没有任何更改。 Why?
为什么?
Only if I try to force reload the property using jQuery it works. 只有当我尝试使用jQuery强制重新加载属性时才有效。
$(document).on('change',"select",function(){
var i = $(this)[0].selectedIndex;
var ch = $(this).children().each(function(index){
$(this).prop('selected',index == i);
if (index == i) {
$(this).attr('selected','selected');
} else {
$(this).removeAttr('selected');
}
});
});
Why? 为什么? How can I avoid this?
我怎么能避免这个? Is it possible to change "selected" using pure html?
是否可以使用纯HTML更改“已选择”?
EDIT I namely want this attrbute in html tag because I need to save and restore the part of this html-code in future. 编辑我想在html标签中这个属性因为我需要在将来保存和恢复这个html代码的一部分。
Updated, substituted .attr("selected", true)
for .prop("selected", true)
更新,替换
.attr("selected", true)
for .prop("selected", true)
Note , value of selected
attribute returns true
or false
, not "selected"
. 注意,
selected
属性的值返回true
或false
,而不是"selected"
。
Try utilizing selector $("option[value=" + this.value + "]", this)
, set property selected
to true
, .siblings()
to remove selected
attribute from option
not selected 尝试使用selector
$("option[value=" + this.value + "]", this)
,将selected
属性设置为true
, .siblings()
从未选中的option
删除selected
属性
$(document).on("change","select",function(){ $("option[value=" + this.value + "]", this) .attr("selected", true).siblings() .removeAttr("selected") });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="lang_select"> <option value="en" selected="true">english</option> <option value="ar">العربية</option> <option value="az">azərbaycanlı</option> <option value="bg">български</option> <option value="ca">català</option> <option value="cs">český</option> <!-- some data cut --> </select>
It's not change the html
itself but it does change the value of the select
它并没有改变
html
本身,但确实改变了select
的值
$('select').change(function(){ $('#result').html($(this).val()); }).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="lang_select"> <option value="en" selected="selected">english</option> <option value="ar">العربية</option> <option value="az">azərbaycanlı</option> <option value="bg">български</option> <option value="ca">català</option> <option value="cs">český</option> <!-- some data cut --> </select> <hr /> <div id="result"></div>
Use the val()
method. 使用
val()
方法。 So in your case, $("#lang_select").val('en');
所以在你的情况下,
$("#lang_select").val('en');
selects the english option. 选择英语选项。 And by the way if you want the first option to be selected you don't need the
selected="selected"
. 顺便说一句,如果你想要选择第一个选项,你不需要
selected="selected"
。 By default the first option is automatically selected. 默认情况下,会自动选择第一个选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.