Javascript 如何分配空值来选择下拉列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26040796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign empty value to select dropdown?
提问by ove
This works with Google Chrome and IE but not Firefox. For some reaons, Chrome and IE allow blank value to be assigned eventhough it is not part of the dropdown selection.
这适用于 Google Chrome 和 IE,但不适用于 Firefox。由于某些原因,Chrome 和 IE 允许分配空白值,即使它不是下拉选择的一部分。
document.getElementById('dropdownid').value = "";
Note that I have access to jQuery but is not doing what i wanted.
请注意,我可以访问 jQuery,但没有做我想做的事。
$("#dropdownid").val("");
The above JQuery does not set it to a blank value because blank is not part of the dropdown options. I need to assign "" to the dropdown selectedvalue due to some third party legacy code issues.
上面的 JQuery 没有将它设置为空白值,因为空白不是下拉选项的一部分。由于某些第三方遗留代码问题,我需要将 "" 分配给下拉选择的值。
Any idea?
任何的想法?
回答by Tetaxa
Setting selectedIndexto -1 clears the selection.
设置selectedIndex为 -1 会清除选择。
document.getElementById('dropdownid').selectedIndex = -1;
回答by Moob
Simply change the selectedIndex. E.G:
只需更改selectedIndex. 例如:
$(function(){
$dropdown = $("#dropdownid");
alert($dropdown.val()); //alerts "2"
$dropdown[0].selectedIndex = -1; //or document.getElementById('dropdownid').selectedIndex = -1;
alert($dropdown.val()) //alerts null
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="dropdownid">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
回答by aafaq ahmad
best way is to use $("#dropdownid").html('');it makes the value null.
最好的方法是使用 $("#dropdownid").html('');它使值为零。
回答by eon
2018 jQuery 3.2.1 answer
2018 jQuery 3.2.1 答案
If you're chaining methods on a jquery selector,
$('#selectId').slideUp(600)[0].selectedIndex = -1appears to work (at least in Chrome) but bothers my 'stick to one syntax/framework' preference.
如果您在 jquery 选择器上链接方法,
$('#selectId').slideUp(600)[0].selectedIndex = -1似乎可以工作(至少在 Chrome 中),但会困扰我的“坚持一种语法/框架”偏好。
This also works:
$('#selectId').slideUp(600).find('option[selected]').attr('selected', false);
这也有效:
$('#selectId').slideUp(600).find('option[selected]').attr('selected', false);
It's a bit longer, but choose the flavor you prefer.
时间有点长,但请选择您喜欢的口味。
回答by CodingSolutions
You can try$(“#elementId”).val('');
e.g.
$(“#CountryDD”).val('');
你可以尝试$(“#elementId”).val('');
例如
$(“#CountryDD”).val('');
回答by Andy
Couldn't you add a blank option to the select before setting it?
在设置之前,您不能在选择中添加一个空白选项吗?
var jqDropdown = $('#dropdownid');
if (!jqDropdown.find('option[value=""]').length) {
jqDropdown.prepend($('<option value=""></option>'));
}
jqDropdown.val("");
回答by Sumit
Have you tried un-selecting the options in the select?
您是否尝试过取消选择选择中的选项?
$("#dropdownid option").prop("selected", false);
$("#dropdownid option").prop("selected", false);
回答by kamran geE
$("#dropdownid option[value='']").attr('selected', true)

