javascript 使用 jQuery 从 COMBO BOX 中获取选定的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18876697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get selected value from COMBO BOX using jQuery
提问by rolu
What I'm trying to do is get the selected x-combo-list-item
, Partnerfrom the x-combo-list
.
How would I do so? Please help. Thank you.
我想要做的就是选择x-combo-list-item
,合作伙伴从x-combo-list
。我该怎么做?请帮忙。谢谢你。
Please refer to my jsfiddle for code reference. --> JSFiddle
请参阅我的 jsfiddle 以获取代码参考。--> JSFiddle
----------------New Question------------------
----------------新问题------------------
Does .each() automatically run when if "Partner" is selected?
如果选择了“合作伙伴”,.each() 是否会自动运行?
回答by Adam van den Hoven
http://jsfiddle.net/littlefyr/H6yeJ/
http://jsfiddle.net/littlefyr/H6yeJ/
JQuery allows you to select based on the content of the element. So you simply use selectors to do what you want:
JQuery 允许您根据元素的内容进行选择。所以你只需使用选择器来做你想做的事:
$('.x-combo-list-item:contains("Partner")').click(function() {
var $this = $(this);
alert('You have selected Partner!');
commonFunction($this);
});
$('.x-combo-list-item:not(:contains("Partner"))').click(function() {
var $this = $(this);
alert('You have not selected Partner!');
commonFunction($this);
});
function commonFunction(item){
// do Ajaxy stuff
};
This fails when you start changing the text (like when you have to translate the text). In this case you simply add a constant value to the tags and use attribute selectors:
当您开始更改文本时(例如您必须翻译文本时),这将失败。在这种情况下,您只需向标签添加一个常量值并使用属性选择器:
$('.x-combo-list-item[data-val=pnr]').click(function() {
var $this = $(this);
alert('You have selected Partner attribute wise!');
commonFunction($this);
});
$('.x-combo-list-item[data-val!=pnr]').click(function() {
var $this = $(this);
alert('You have not selected Partner attribute wise!');
commonFunction($this);
});
$('.x-combo-list-item:not([data-val=pnr])').click(function() {
var $this = $(this);
alert('You have not selected Partner alternative attribute wise!');
commonFunction($this);
});
You also can combine those with .x-combo-selected
and :not(.x-combo-selected)
in order to handle selected items differently.
您还可以将它们与.x-combo-selected
和结合起来:not(.x-combo-selected)
,以不同方式处理所选项目。
If you're adding items via code (or even as a matter of principle) you should delegate the events to a relevant ancestor:
如果您通过代码添加项目(或者甚至作为原则问题),您应该将事件委托给相关的祖先:
$('.x-combo-list-inner')
.on('click', '.x-combo-list-item:contains("Partner")',function() {
var $this = $(this);
alert('You have selected Partner! Again');
commonFunction($this);
}).on('click', '.x-combo-list-item:not(:contains("Partner"))', function() {
var $this = $(this);
alert('You have not selected Partner! again');
commonFunction($this);
})
回答by CodingIntrigue
If I understand correctly, you want to alert
when the user clicks on the div
which contains the text partner?
如果我理解正确,您希望alert
当用户单击div
其中包含文本伙伴时?
$('.x-combo-list-item').click(function() {
if ($(this).text() === "Partner") {
alert('You have selected Partner!');
// Fire your ajax call here
/*
$.post('handler.php', {data: data : {even: 'more data'}}, function(data), 'json');
*/
}
});
You had a call to retrieve data-item
which doesn't exist, so I'm not entirely sure.
你有一个data-item
不存在的检索电话,所以我不完全确定。
回答by ThatGuyGrant
Try This:
试试这个:
$('#ext-1111 div').each(function() {
if (jQuery(this).html() == "Partner") {
alert("This is Partner")
}
});
Regards
问候
回答by Akash Sarawagi
$('id if box').val('Partner') ; This will do.
$('id if box').val('Partner') ; 这会做。