如何从ID中获取值

时间:2012-10-06 22:11:57

标签: jquery

如何在点击时获取单选按钮ID的值。

<script>
    $(document).ready(function() {      
        $('label').click(function() {           
            var total = 0;              
            $('.option:checked').each(function() {
                total += parseInt($(this).val());
            });             
            $('.sub-total-t').html('$' + total);                
        });
    });

</script>     

此脚本适用于value - &gt;如何分配ID

<label><input type="radio" name="print" class="option" id="25" value="p10"/> Starter 500 </label>

3 个答案:

答案 0 :(得分:1)

尝试这样做:

$('.option:checked').each(function() {
    total += parseInt($(this).attr('id'));
});

但最好使用data属性来存储这种值:

<input data-number="25" /> 

要在javascript中检索它,请使用:

$(this).data('number')

对于number,您可以使用所需的每个名称。

答案 1 :(得分:0)

使用此:

 total += parseInt($(this).attr("id"));

但是,元素不应该将数字作为id。 ID值应以字母开头。

答案 2 :(得分:0)

$('input[type=radio]:checked').attr('id');

试试这个..

在这种情况下

    total += parseInt($(this).attr('id'));

OR 
  total += parseInt(this.id);  // This is faster
相关问题