[英]jQuery get specific option tag selected values
I have this, 我有这个,
<select id="customUser_id" name="customUser_id">
<option value="2" label="Friends Of Friends">Friends Of Friends</option>
<option selected="selected" value="3" label="Friends Only">Friends Only</option>
<option value="4" label="Specific People">Specific People</option>
<option value="0" label="Only Me">Only Me</option>
</select>
I want to get the value of Selected Option
using jQuery. 我想使用jQuery获取
Selected Option
的值。
Currently I am using onchange()
event like this, 目前我正在使用像这样的
onchange()
事件,
$('#customUser_id').change(function(){
if(($(this).val()==2)){
$("#customWallPost3").text("Only Friends of Friends can see this");
}
else if(($(this).val()==3)){
$("#customWallPost3").text("Only Friends can see this");
}
else if(($(this).val()==4)){
$("#customWallPost3").text("Only the people above can see this");
}
else if(($(this).val()==0)){
$("#customWallPost3").text("Only I can see this");
}
else{
$("#customWallPost3").text("");
}
});
this is only working when change the option value, but I also want to get selected option value with this. 这仅在更改选项值时有效,但我也希望用此选项获取选项值。 Thanks in advance
提前致谢
not sure what your are asking for... 不确定你的要求是什么......
I also want to get selected option value with this..
我也希望用这个选择值。
but to get the selected option value, you use 但要获取所选的选项值,请使用
$('#customUser_id').val();
this gives you the selected value of the <select>
with id as customUser_id
这将为您提供
<select>
的选定值,其id为customUser_id
UPDATED 更新
if you want to get the text of selected option then you can do 如果你想获得所选选项的文本,那么你可以这样做
$("#customUser_id option:selected").text();
Unsure with your question as $('#customUser_id').val()
gives the value which is selected. 不确定您的问题是
$('#customUser_id').val()
给出选择的值。
Still if you wish, with this you can iterate through each of the options and you can get the element with selected
attribute 如果你愿意,你仍然可以遍历每个选项,你可以获得具有
selected
属性的元素
$('#customUser_id').children().each(function() {
if($(this).attr('selected')) {
// here you have the element with _selected_ attribute
}
});
If you wish you can directly get the attribute
which is selected 如果您愿意,可以直接获取所选的
attribute
var selectedElt = $('#customUser_id').children('[selected=selected]');
you can create it in a more friendly way - using an array and a function: 你可以用更友好的方式创建它 - 使用数组和函数:
var arr_Infos = [
'Only I can see this', // option value 0
'', // option value 1
'Only Friends of Friends can see this', // option value 2
'Only Friends can see this', // option value 3
'Only the people above can see this' // option value 4 (don't use comma)
];
function popoulateInfo(){
var sel = $('#customUser_id').find(":selected").val();
$('#customWallPost3').text( arr_Infos[ sel ] );
}
popoulateInfo(); // run immediately
$('#customUser_id').on('change', function(){
popoulateInfo(); // run on change
});
Try this to get value of selected value: 试试这个来获取所选值的值:
$('#customUser_id').val();
Here is the docs http://api.jquery.com/val/ 这是文档http://api.jquery.com/val/
You mean if you want the selected option to display - "Only I can see this" or "Only friends can see this" etc.... on load, You can add this code with .ready()
instead on .change()
你的意思是,如果你想要显示所选的选项 - “只有我能看到这个”或“只有朋友可以看到这个”等....加载时,你可以用
.ready()
代替.change()
来添加这个代码
$('#customUser_id').ready(function(){
if(($(this).val()==2)){
$("#customWallPost3").text("Only Friends of Friends can see this");
}
else if(($(this).val()==3)){
$("#customWallPost3").text("Only Friends can see this");
}
else if(($(this).val()==4)){
$("#customWallPost3").text("Only the people above can see this");
}
else if(($(this).val()==0)){
$("#customWallPost3").text("Only I can see this");
}
else{
$("#customWallPost3").text("");
}
});
use trigger 使用触发器
$('#customUser_id').change(function(){
// your code
}).trigger('change');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.