jQuery 重新填充下拉列表选项,如何清除选项列表然后重新填充?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4136092/
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
Re-populate drop down list options, how to clear options list and then re-populate?
提问by Blankman
Re-populate drop down list options, how to clear options list and then re-populate?
重新填充下拉列表选项,如何清除选项列表然后重新填充?
When a event fires, I need to wipe out the current contents of the drop down list #users, and then re-populate it via ajax.
当事件触发时,我需要清除下拉列表#users 的当前内容,然后通过ajax 重新填充它。
My ajax call is returning the HTML for the options like:
我的 ajax 调用正在返回以下选项的 HTML:
<option name=blah1>text1</option>
<option name=blah2>text2</option>
<option name=blah3>text3</option>
回答by kobe
you can use the below
你可以使用下面的
$('#mySelect').empty()
and then rebind the new data.
然后重新绑定新数据。
回答by hunter
You can just change the html
of the select
element using the data returned from your ajax call
你可以只改变html
了的select
利用你的Ajax调用返回的数据元素
$("#users").html(data);
so it would look something like this:
所以它看起来像这样:
$.ajax({
type: "POST",
url: url,
data: data,
success: : function(data) {
$("#users").html(data);
}
dataType: "HTML"
});
回答by Gajendra Bang
function(ajaxResult) {
$('#users').html(""); //clear old options
$('#users').html(ajaxResult);
}
回答by shoebox639
In the call back of your AJAX call, just clear out the contents of your select element and re-add those returned by the AJAX call.
在 AJAX 调用的回调中,只需清除 select 元素的内容并重新添加 AJAX 调用返回的内容。
function(ajaxResult) {
$('#users').html(ajaxResult);
}