문제

Using this select list as an example:

<select name="my-list" id="my-list">
    <option value="Name 1">Simon</option>
    <option value="Name 2">Frank</option>
    <option value="Name 3">Bob</option>
    <option value="Name 4">Alex</option>
</select>

Using jQuery, I need to blank out all available option text displayed (Simon, Frank, Bob and Alex), i.e. I am after the following instead:

<select name="my-list" id="my-list">
    <option value="Name 1"></option>
    <option value="Name 2"></option>
    <option value="Name 3"></option>
    <option value="Name 4"></option>
</select>
도움이 되었습니까?

해결책

Try

fiddle Demo

$('#my-list option').text('');


.text()

$('#my-list option') -->refers to option inside element with id my-list

$('#my-list option').text(''); set text to ''

다른 팁

The proper way would be, But keep in mind that .empty() will also remove the html nodes inside the target element. I am suggesting this because you wanted to clear the contents inside the option tag.

$('#my-list option').empty();

Please read this : http://api.jquery.com/empty/

DEMO

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top