开发者

How to get the text value of a selected item from a DataList using jQuery

How do I get the text value of the options in a DataList? I need to make use of the 开发者_如何学JAVAvalue of an id, But I also need the value of the selectedIndex, which is the name.

<input type="text" name="names[]" id="names" list="neym"/>
<datalist id="neym">
    <option value="example"></option>
    <option value="example2"></option>
    <option value="example3"></option>
</datalist>

How do I do that in jQuery?


Loop through them and use text() and val() as others have pointed out:

$('#neym option').each(function(index) {
    var id = $(this).val();
    var name = $(this).text();

    // Do something with the id and name
    alert('Found ' + name + ' with id ' + id);
});


For getting selected index's text

$("#neym option:selected").text()

For getting selected index's value

$('#neym').attr('value')


Considering an input with a datalist data >>

to get val() try:

$("#inputid").val();

to get text() try:

$("#datalistid option[value='" + $('#inputid').val() + "']").text();


To get the text inside the selected option

$( "#neym :selected" ).text();

Or to get it's value

$( "#neym :selected" ).val()


<input id="option_box" list="my_options">
<datalist id="my_options">
</datalist>

document.getElementById('option_box').value;

The input element holds the value of the selected field in the datalist. This is a simple non jquery version of the answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜