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.
这适用于谷歌Chrome和IE,但不适用于Firefox。对于一些reaons, 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没有将它设置为空白值,因为空格不是下拉选项的一部分。由于一些第三方遗留代码问题,我需要将“”分配给下拉的selectedvalue。
Any idea?
任何想法?
5 个解决方案
#1
5
Setting selectedIndex
to -1 clears the selection.
将selectedIndex设置为-1将清除所选内容。
document.getElementById('dropdownid').selectedIndex = -1;
#2
2
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>
http://jsfiddle.net/kxoytdg8/
#3
0
Have you tried un-selecting the options in the select?
你试过在选择中取消选择选项吗?
$("#dropdownid option").prop("selected", false);
$(" # dropdownid选项”)。道具(“选择”,假);
#4
0
Couldn't you add a blank option to the select before setting it?
难道你不能在设置之前给select添加一个空格选项吗?
var jqDropdown = $('#dropdownid');
if (!jqDropdown.find('option[value=""]').length) {
jqDropdown.prepend($('<option value=""></option>'));
}
jqDropdown.val("");
#5
0
best way is to use $("#dropdownid").html('');
it makes the value null.
最好的方法是使用$(“#dropdownid”).html(“);它使值为空。
#1
5
Setting selectedIndex
to -1 clears the selection.
将selectedIndex设置为-1将清除所选内容。
document.getElementById('dropdownid').selectedIndex = -1;
#2
2
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>
http://jsfiddle.net/kxoytdg8/
#3
0
Have you tried un-selecting the options in the select?
你试过在选择中取消选择选项吗?
$("#dropdownid option").prop("selected", false);
$(" # dropdownid选项”)。道具(“选择”,假);
#4
0
Couldn't you add a blank option to the select before setting it?
难道你不能在设置之前给select添加一个空格选项吗?
var jqDropdown = $('#dropdownid');
if (!jqDropdown.find('option[value=""]').length) {
jqDropdown.prepend($('<option value=""></option>'));
}
jqDropdown.val("");
#5
0
best way is to use $("#dropdownid").html('');
it makes the value null.
最好的方法是使用$(“#dropdownid”).html(“);它使值为空。