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

时间:2013-09-18 15:42:48

标签: javascript jquery ajax combobox

我要做的是从x-combo-list-item获取所选的x-combo-list合作伙伴。 我该怎么办?请帮忙。谢谢。

请参阅我的jsfiddle以获取代码参考。 - > JSFiddle

----------------新问题------------------

如果"合作伙伴"是否会自动运行.each()被选中了?

4 个答案:

答案 0 :(得分:4)

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

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
};

当您开始更改文本时(例如,您必须翻译文本时),此操作会失败。在这种情况下,您只需向标记添加常量值并使用属性选择器:

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

您还可以将这些内容与.x-combo-selected:not(.x-combo-selected)结合使用,以便以不同方式处理所选项目。

如果您是通过代码添加项目(或者甚至是原则上的),您应该将事件委托给相关的祖先:

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

答案 1 :(得分:2)

如果我理解正确,当用户点击包含文字合作伙伴的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');
        */
    }
});

您有一个要求检索不存在的data-item的电话,所以我不完全确定。

答案 2 :(得分:0)

试试这个:

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

此致

答案 3 :(得分:0)

$('id if box')。val('Partner'); 这样做。

相关问题