I am attempting to retrieve and set the selected value of a select element (drop down list) with jQuery.
我正在尝试使用jQuery检索并设置选择元素(下拉列表)的值。
for retrievel i have tried $("#myId").find(':selected').val()
, as well as $("#myId").val()
but both return undefined.
对于retrievel,我尝试了$(“#myId”).find(':selected').val(),以及$(“#myId”).val(),但它们都返回未定义。
Any insight into this problem would be much appreciated.
对这个问题的任何见解都将非常感谢。
thanks in advance
谢谢提前
8 个解决方案
#1
145
The way you have it is correct at the moment. Either the id of the select is not what you say or you have some issues in the dom.
你现在的方法是正确的。选择的id不是您所说的,或者您在dom中有一些问题。
Check the Id of the element and also check your markup validates at here at W3c.
检查元素的Id,并在W3c检查标记验证。
Without a valid dom jQuery cannot work correctly with the selectors.
没有有效的dom jQuery就不能正确地使用选择器。
If the id's are correct and your dom validates then the following applies:
如果id是正确的,并且您的dom是有效的,则应用以下方法:
To Read Select Option Value
读取选择选项值
$('#selectId').val();
To Set Select Option Value
设置选择选项值
$('#selectId').val('newValue');
To Read Selected Text
读取选中的文本
$('#selectId>option:selected').text();
#2
248
to get/set the actual selectedIndex property of the select element use:
要获取/设置select元素使用的实际selectedIndex属性:
$("#select-id").prop("selectedIndex");
$("#select-id").prop("selectedIndex",1);
#3
7
$('#myId').val()
should do it, failing that I would try:
$('#myId').val()应该这样做,否则我将尝试:
$('#myId option:selected').val()
#4
4
When setting with JQM
, don't forget to update the UI
:
使用JQM设置时,不要忘记更新UI:
$('#selectId').val('newValue').selectmenu('refresh', true);
#5
3
$("#myId").val()
should work if myid
is the select element id!
如果myId是select元素id,那么$("#myId").val()应该是有效的!
This would set the selected item: $("#myId").val('VALUE');
这将设置选中的项:$(“#myId”).val(“值”);
#6
1
Suppose you have created a Drop Down list using SELECT tag like as follows,
假设您使用SELECT标记创建了一个下拉列表,如下所示,
<select id="Country">
Now if you want to see what is the selected value from drop down using JQuery then, simply put following line to retrieve that value..
现在,如果您想要查看使用JQuery从下拉列表中选择的值是什么,那么只需使用以下行来检索该值。
var result= $("#Country option:selected").text();
it will work fine.
它会正常工作。
#7
0
I know this is old but I just had a bear of a time with Razor, could not get it to work no matter how hard I tried. Kept coming back as "undefined" no matter if I used "text" or "html" for attribute. Finally I added "data-value" attribute to the option and it read that just fine.
我知道这是旧的,但我有一段时间的熊与剃刀,无法使它工作,无论我如何努力。不管我是用“text”还是“html”来表示属性,它总是以“undefined”返回。最后,我为选项添加了“data-value”属性,它读起来很好。
<option value="1" data-value="MyText">MyText</option>
var DisplayText = $(this).find("option:selected").attr("data-value");
#8
0
$( "#myId option:selected" ).text(); will give you the text that you selected in the drop down element. either way you can change it to .val(); to get the value of it . check the below coding
$(" # myId选项:选择")。text();将会给您在下拉元素中选择的文本。无论哪种方式,都可以将其更改为.val();得到它的值。检查下面的编码
<select id="myId">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>`
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>
#1
145
The way you have it is correct at the moment. Either the id of the select is not what you say or you have some issues in the dom.
你现在的方法是正确的。选择的id不是您所说的,或者您在dom中有一些问题。
Check the Id of the element and also check your markup validates at here at W3c.
检查元素的Id,并在W3c检查标记验证。
Without a valid dom jQuery cannot work correctly with the selectors.
没有有效的dom jQuery就不能正确地使用选择器。
If the id's are correct and your dom validates then the following applies:
如果id是正确的,并且您的dom是有效的,则应用以下方法:
To Read Select Option Value
读取选择选项值
$('#selectId').val();
To Set Select Option Value
设置选择选项值
$('#selectId').val('newValue');
To Read Selected Text
读取选中的文本
$('#selectId>option:selected').text();
#2
248
to get/set the actual selectedIndex property of the select element use:
要获取/设置select元素使用的实际selectedIndex属性:
$("#select-id").prop("selectedIndex");
$("#select-id").prop("selectedIndex",1);
#3
7
$('#myId').val()
should do it, failing that I would try:
$('#myId').val()应该这样做,否则我将尝试:
$('#myId option:selected').val()
#4
4
When setting with JQM
, don't forget to update the UI
:
使用JQM设置时,不要忘记更新UI:
$('#selectId').val('newValue').selectmenu('refresh', true);
#5
3
$("#myId").val()
should work if myid
is the select element id!
如果myId是select元素id,那么$("#myId").val()应该是有效的!
This would set the selected item: $("#myId").val('VALUE');
这将设置选中的项:$(“#myId”).val(“值”);
#6
1
Suppose you have created a Drop Down list using SELECT tag like as follows,
假设您使用SELECT标记创建了一个下拉列表,如下所示,
<select id="Country">
Now if you want to see what is the selected value from drop down using JQuery then, simply put following line to retrieve that value..
现在,如果您想要查看使用JQuery从下拉列表中选择的值是什么,那么只需使用以下行来检索该值。
var result= $("#Country option:selected").text();
it will work fine.
它会正常工作。
#7
0
I know this is old but I just had a bear of a time with Razor, could not get it to work no matter how hard I tried. Kept coming back as "undefined" no matter if I used "text" or "html" for attribute. Finally I added "data-value" attribute to the option and it read that just fine.
我知道这是旧的,但我有一段时间的熊与剃刀,无法使它工作,无论我如何努力。不管我是用“text”还是“html”来表示属性,它总是以“undefined”返回。最后,我为选项添加了“data-value”属性,它读起来很好。
<option value="1" data-value="MyText">MyText</option>
var DisplayText = $(this).find("option:selected").attr("data-value");
#8
0
$( "#myId option:selected" ).text(); will give you the text that you selected in the drop down element. either way you can change it to .val(); to get the value of it . check the below coding
$(" # myId选项:选择")。text();将会给您在下拉元素中选择的文本。无论哪种方式,都可以将其更改为.val();得到它的值。检查下面的编码
<select id="myId">
<option value="1">Mr</option>
<option value="2">Mrs</option>
<option value="3">Ms</option>`
<option value="4">Dr</option>
<option value="5">Prof</option>
</select>