簡體   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();
    }
));

如何將字段類型識別為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
});

小提琴

你有一些錯誤的語法,在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();
});

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM