簡體   English   中英

使用jQuery從COMBO BOX獲取選定的值

[英]Get selected value from COMBO BOX using jQuery

我想要做的就是所選擇的x-combo-list-item從, 合作伙伴 x-combo-list 我該怎么辦? 請幫忙。 謝謝。

請參考我的jsfiddle以獲得代碼參考。 -> JSFiddle

----------------新問題------------------

如果選擇了“合作伙伴”,.each()是否自動運行?

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

如果我理解正確,您想在用戶單擊包含文本伙伴的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');
        */
    }
});

您打電話來檢索不存在的data-item ,所以我不確定。

嘗試這個:

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

問候

$('id if box')。val('Partner'); 這樣就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM