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

javascript - jQuery Selector - checked and class begins with - Stack Overflow

programmeradmin1浏览0评论

I need to get the value of the first checkbox which is checked and who's class name begins with 'rqc', eg. rqc205

I have tried this: requestID=$('#requestsTable').find('input[class^='rqc']:checked').val();

but it yields nothing, whereas requestID=$('#requestsTable').find('input:checked').val(); works but does not limit to the class.

I need to get the value of the first checkbox which is checked and who's class name begins with 'rqc', eg. rqc205

I have tried this: requestID=$('#requestsTable').find('input[class^='rqc']:checked').val();

but it yields nothing, whereas requestID=$('#requestsTable').find('input:checked').val(); works but does not limit to the class.

Share Improve this question edited Mar 7, 2010 at 2:04 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Mar 7, 2010 at 1:51 AlexAlex 1,5782 gold badges11 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You have a syntax error. Either fix your quotes around the attribute value, or remove them altogether (they don't seem to be required).

requestID = $('#requestsTable').find("input[class^='rqc']:checked").val();

or

requestID = $('#requestsTable').find("input[class^=rqc]:checked").val();

Perhaps:

requestID = $('#requestsTable').find('input[class^="rqc"]').filter(':checked').val();

This is, currently un-checked/un-verified. I think it should work, but I'm still new to jQuery, so I'll post the possibility, check it and then amend if necessary.


Much to my surprise, that works.

But it's worth pointing out, I think, that it might be easier to get the value using jQuery's click() event.

$(document).ready(
    function() {
        $('input[class^="rqc"]').click(
            function() {
                var requestID = $(this).val();
            }
        );

    }
);

Though obviously that depends on what exactly you're doing, and why.

发布评论

评论列表(0)

  1. 暂无评论