开发者

Using the Ends With jquery selector and get the options of a listbox

I have the following code:

$("input[id$=UserField_hiddenSpanData],input[title=Title]").each(function(){

    var rb = $('#ctl00_m_g_c6ae303a_6013_4adb_8057_63a214bcfd24_ctl00_ctl04_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_SelectResult option').length;
    var val = $(this).val();
 开发者_Python百科   if(val != 0 && val.length != 0 && rb != 0) { 

        //add one to the counter
        controlsPassed += 1;
    }

    });

It works fine but I don't want to have the ID hardcoded so I thought I could use

var rb = $('id$=SelectResult option').length;

but it doesn't work, what's wrong with my syntax?

Thanks in advance.


You need brackets on your ends-with selector, like this:

var rb = $('[id$=SelectResult] option').length;

It'll be faster with the element selector on there though:

var rb = $('select[id$=SelectResult] option').length;

Another thought as stated in comments on a prior question, there's no need to repeat this check every time in the loop, you can do it outside once :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜