如何使用jquery检查输入类型是否为广播

时间:2022-09-11 08:04:12

I want to handle html elements differently based on their type.

我希望根据html元素的类型以不同的方式处理它们。

Using jquery, how do I check to see if an input type is a radio button?

使用jquery,如何检查输入类型是否为单选按钮?

I've tried:

我试过了:

if ($('#myElement').is(':radio')) {
    ....//code here
}

and

if ($('#myElement').is("input[type='radio']")) {
    ....//code here
}

Neither of these worked. Any ideas?

这两个工作。什么好主意吗?

EDIT:

编辑:

if ($('#myElement').is(':radio')) {
    ....//code here
}

works, but my radio buttons don't have the id attribute, they only have a name attribute, which is why it did not work.

可以,但是我的单选按钮没有id属性,它们只有name属性,这就是为什么它不能工作的原因。

I changed my code to:

我把代码改为:

if ($('input[name=' + myElement + ']').is(":radio")) {
    ....//code here
}

3 个解决方案

#1


42  

That should work as long as the elements are loaded.

只要元素被加载,它就可以工作。

// Ensure the DOM is ready
$(function() {
    if ($('#myElement').is(':radio')) {
        //code here
    }
});

If you're assigning handlers based on type, another approach would be to use .filter().

如果根据类型分配处理程序,另一种方法是使用.filter()。

$(function() {
    var el = $('#myElement');
    el.filter(':radio').change(function() {
        //code here
    });

    el.filter(':checkbox').change(function() {
        // other code here
    });
});

If it doesn't pass the filter(), the handler won't be assigned.

如果它没有通过filter(),处理程序将不会被分配。

#2


1  

That should work you can try to use filter this way instead

这样你就可以用这种方法来代替。

if ($('#myElement').filter(':radio').size() > 0) {
    ....//code here
}
and

if ($('#myElement').filter("input[type='radio']").size() > 0) {
    ....//code here
}

Or

$('#myElement:radio').each(function() {
        ....//code here
});

Udate: Are you also sure that the id is unique in the page? If you want to select more as 1 element you can use a class instead.

Udate:你是否也确定这个id在页面中是唯一的?如果你想选择更多的元素作为一个元素,你可以使用一个类来代替。

#3


1  

if($('#my-element').attr('type') == 'radio')

#1


42  

That should work as long as the elements are loaded.

只要元素被加载,它就可以工作。

// Ensure the DOM is ready
$(function() {
    if ($('#myElement').is(':radio')) {
        //code here
    }
});

If you're assigning handlers based on type, another approach would be to use .filter().

如果根据类型分配处理程序,另一种方法是使用.filter()。

$(function() {
    var el = $('#myElement');
    el.filter(':radio').change(function() {
        //code here
    });

    el.filter(':checkbox').change(function() {
        // other code here
    });
});

If it doesn't pass the filter(), the handler won't be assigned.

如果它没有通过filter(),处理程序将不会被分配。

#2


1  

That should work you can try to use filter this way instead

这样你就可以用这种方法来代替。

if ($('#myElement').filter(':radio').size() > 0) {
    ....//code here
}
and

if ($('#myElement').filter("input[type='radio']").size() > 0) {
    ....//code here
}

Or

$('#myElement:radio').each(function() {
        ....//code here
});

Udate: Are you also sure that the id is unique in the page? If you want to select more as 1 element you can use a class instead.

Udate:你是否也确定这个id在页面中是唯一的?如果你想选择更多的元素作为一个元素,你可以使用一个类来代替。

#3


1  

if($('#my-element').attr('type') == 'radio')