开发者

How do i select and replace <option>?

I have a <select> field.

<select name="select-states" disabled="disabled"><option value="">Choose State &raquo;</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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜