简体   繁体   中英

Get matched element from the selectors used in jquery.is function

$("#see1, #seeAlso1, .see").is(':checked')

The above code will return a boolean value. Is there any way to get first matched element which will return 'true'....?

您可以使用:checked选择器和first()来获取页面中第一个找到的元素:

var $el = $("#see1:checked, #seeAlso1:checked, .see:checked").first();

You can use $.each :

function getFirstChecked() {
    var firstChecked = null;

    $("#see1, #seeAlso1, .see").each(function(this, index){
        var $this = $(this);

        if ($this.is(':checked')) {
            firstChecked = $this;
        }
    });

    return firstChecked;
}

Edit:

Better solution:

$("#see1, #seeAlso1, .see").filter(':checked').eq(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM