How do i select and replace <option>?
I have a <select>
field.
<select name="select-states" disabled="disabled"><option value="">Choose State »</option></select>
I would like to replace just the <option>
inside the <select>
with the response received from Ajax Script which contains multiple elements.
I would like to know the selector for selecting <option>
which is a c开发者_如何学Pythonhild of <select name = "select-states">
without using the id attribute?
$('select[name=select-states] option');
to get all options
$('select[name=select-states] option')
to get selected option
$('select[name=select-states] option:selected')
to replace its options with ajax response
$('select[name=select-states]').html(AjaxResponseHtml)
Like this:
$('select[name="select-states"] option')
If you want to replace the <option>
element (and anything that's inside the <select>
), you should use:
$('select[name=select-states]').html(ajaxResultHere);
$(":select[name='elect-states']").find("option")
Do this:
$('select[name="elect-states"]').empty().append(function() {
var str = '';
// construct HTML string from Ajax-response data
return str;
});
$('select[name="select-states"] option').replaceWith(jQueryAjaxWrapper);
Where jQueryAjaxWrapper
is the result of your AJAX call wrapped in jQuery :P
精彩评论