最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get selected value from COMBO BOX using jQuery - Stack Overflow

programmeradmin0浏览0评论

What I'm trying to do is get the selected x-bo-list-item, Partner from the x-bo-list. How would I do so? Please help. Thank you.

Please refer to my jsfiddle for code reference. --> JSFiddle

----------------New Question------------------

Does .each() automatically run when if "Partner" is selected?

What I'm trying to do is get the selected x-bo-list-item, Partner from the x-bo-list. How would I do so? Please help. Thank you.

Please refer to my jsfiddle for code reference. --> JSFiddle

----------------New Question------------------

Does .each() automatically run when if "Partner" is selected?

Share Improve this question edited Sep 18, 2013 at 16:32 rolu asked Sep 18, 2013 at 15:42 rolurolu 3982 gold badges4 silver badges20 bronze badges 2
  • 2 The simple way: pare item test with the string "Partner": jsfiddle/MkA4T/26 – pawel Commented Sep 18, 2013 at 15:46
  • Works great, change has been update. Appreciate the quick response. – rolu Commented Sep 18, 2013 at 15:55
Add a ment  | 

4 Answers 4

Reset to default 4

http://jsfiddle/littlefyr/H6yeJ/

JQuery allows you to select based on the content of the element. So you simply use selectors to do what you want:

$('.x-bo-list-item:contains("Partner")').click(function() {
    var $this = $(this);
    alert('You have selected Partner!');
    monFunction($this);
});

$('.x-bo-list-item:not(:contains("Partner"))').click(function() {
    var $this = $(this);
    alert('You have not selected Partner!');
    monFunction($this);
});

function monFunction(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-bo-list-item[data-val=pnr]').click(function() {
    var $this = $(this);
    alert('You have selected Partner attribute wise!');
    monFunction($this);
});

$('.x-bo-list-item[data-val!=pnr]').click(function() {
    var $this = $(this);
    alert('You have not selected Partner attribute wise!');
    monFunction($this);
});
$('.x-bo-list-item:not([data-val=pnr])').click(function() {
    var $this = $(this);
    alert('You have not selected Partner alternative attribute wise!');
    monFunction($this);
});

You also can bine those with .x-bo-selected and :not(.x-bo-selected) in order to handle selected items differently.

If you're adding items via code (or even as a matter of principle) you should delegate the events to a relevant ancestor:

$('.x-bo-list-inner')
.on('click', '.x-bo-list-item:contains("Partner")',function() {
    var $this = $(this);
    alert('You have selected Partner! Again');
    monFunction($this);
}).on('click', '.x-bo-list-item:not(:contains("Partner"))', function() {
    var $this = $(this);
    alert('You have not selected Partner! again');
    monFunction($this);
})

If I understand correctly, you want to alert when the user clicks on the div which contains the text partner?

$('.x-bo-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.

Try This:

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

Regards

$('id if box').val('Partner') ; This will do.

发布评论

评论列表(0)

  1. 暂无评论