简体   繁体   English

使用jQuery从COMBO BOX获取选定的值

[英]Get selected value from COMBO BOX using jQuery

What I'm trying to do is get the selected x-combo-list-item , Partner from the x-combo-list . 我想要做的就是所选择的x-combo-list-item从, 合作伙伴 x-combo-list How would I do so? 我该怎么办? Please help. 请帮忙。 Thank you. 谢谢。

Please refer to my jsfiddle for code reference. 请参考我的jsfiddle以获得代码参考。 --> JSFiddle -> JSFiddle

----------------New Question------------------ ----------------新问题------------------

Does .each() automatically run when if "Partner" is selected? 如果选择了“合作伙伴”,.each()是否自动运行?

http://jsfiddle.net/littlefyr/H6yeJ/ http://jsfiddle.net/littlefyr/H6yeJ/

JQuery allows you to select based on the content of the element. JQuery允许您根据元素的内容进行选择。 So you simply use selectors to do what you want: 因此,您只需使用选择器即可执行所需的操作:

$('.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);
})

If I understand correctly, you want to alert when the user clicks on the div which contains the text partner? 如果我理解正确,您想在用户单击包含文本伙伴的div时发出alert吗?

$('.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 ,所以我不确定。

Try This: 尝试这个:

$('#ext-1111 div').each(function() {
  if (jQuery(this).html() == "Partner") {
    alert("This is Partner")
  }
});

Regards 问候

$('id if box').val('Partner') ; $('id if box')。val('Partner'); This will do. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM