简体   繁体   English

如何在jQuery中获取输入类型复选框和收音机的类型

[英]how to get types of input-type checkbox and radio in jQuery

jQuery('.pricefield select','.pricefield input').each
    (jQuery(this).change (function (){
        alert(jQuery(this).attr("type")); //it give undefined
        alert(jQuery(this).is(':input')); //it gives false
        var allInputs = jQuery(":input"); 
        alert(allInputs.attr('type'));  //it gives hidden
        get_sum();
    }
));

How can I can identify the field type as select or radio or checkbox ?如何将字段类型识别为selectradiocheckbox

jQuery('select, input', '.pricefield').on('change', function () {
     var tag  = $(this).prop('tagName'); // return "select", "input" etc
     var type = $(this).prop('type'); // return "radio", "checkbox" etc
});

FIDDLE小提琴

You had some wrong syntax, missing a function declaration inside the each() , but actually you can write it more simple, like:你有一些错误的语法,在each()中缺少一个函数声明,但实际上你可以把它写得更简单,比如:

jQuery('.pricefield select, .pricefield input').change(function () {
    alert(jQuery(this).attr("type"));
    alert(jQuery(this).is(':input')); 
    var allInputs = jQuery(":input");
    alert(allInputs.attr('type'));
    get_sum();
});

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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