【问题标题】:jquery checkbox attribute valuejquery复选框属性值
【发布时间】:2010-06-25 18:19:33
【问题描述】:

我有一个循环并产生多个结果的跨度。
我想要的是属性recordID检查时的值

<span  id="row" index="<?php print $i; ?>" recordID="<?php print $row->ID; ?>"  Color="<?php print $row->Color; ?>">  
<input type="checkbox" style="left:10;" />  
</span>

我在我的 js 文件中使用它来检查复选框是否被点击

var test =  $('input:checkbox:checked').val();      
alert(test);

我得到的只是开启
如何获取属性值
谢谢

【问题讨论】:

    标签: php jquery-ui jquery


    【解决方案1】:

    这将为您获取属性值

    $('#row').attr('recordID');
    

    如果您想在复选框被选中时获取值,这里是一个示例

    js 文件

    $(document).ready(function(){
     $('input:checkbox').change(function(){
      if($(this).attr('checked')){
       alert($(this).parent().attr('recordID'));
      }
     });
    });
    

    查看已检查的行数:

    JavaScript

    $(document).ready(function(){
     $('#check').click(function(event){
      event.preventDefault();
      alert($('input:checkbox:checked').size());
     });
    });
    

    HTML

    <span  id="row1" recordID="1">  
     <input type="checkbox" style="left:10;" />  
     <input type="checkbox" style="left:10;" />  
     <input type="checkbox" style="left:10;" />  
     <input type="checkbox" style="left:10;" />  
    </span>
    <span  id="row2" recordID="2">  
     <input type="checkbox" style="left:10;" />  
    </span>
    <span  id="row3" recordID="3">  
     <input type="checkbox" style="left:10;" />  
    </span>
    <span  id="row4" recordID="4">  
     <input type="checkbox" style="left:10;" />  
    </span>
    <button id='check'>Check Num Rows</button>
    

    在单个跨度内检查

    $(document).ready(function(){
        $('#check').click(function(event){
            event.preventDefault();
            alert($('#row1 input:checkbox:checked').size());
        });
    });
    

    这是我用来制作您需要的示例的完整示例代码

    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    
    <script type='text/javascript'>
    $(document).ready(function(){
        $('input:checkbox').change(function(){
            alert($(this).parent().find(':checkbox:checked').size());
        });
    });
    </script>
    </head>
    <body>
    <span  id="row1" recordID="1">  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
    </span><br/><br/>
    <span  id="row2" recordID="2">  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
    </span><br/><br/>
    <span  id="row3" recordID="3">  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
    </span><br/><br/>
    <span  id="row4" recordID="4">  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
        <input type="checkbox" style="left:10;" />  
    </span>
    </body>
    </html>
    

    【讨论】:

    • 我可以使用 alert($("input[type=checkbox]:checked").length);但我希望只能在 Span id=row 内检查。我该怎么做谢谢
    • wowo_999 感谢您的帮助。但由于某种原因,它并没有给我一个跨度内选中的框总数,谢谢
    • 在上面列出的代码中,我将其绑定到单击。如果您不想将其绑定到任何东西,则可以将其删除,但是 alert($('#row1 input:checkbox:checked').size());将打印 id="row1" 的行的值
    • 警报给我 0。我试了几次,但没有成功。谢谢!
    • 我在下面使用了所有这些,但没有运气 alert($("#row input[type=checkbox]:checked").length); alert('Row1 id: '+$('#row1 input:checkbox:checked').size()); alert('Proc Class '+$('#proc input:checkbox:checked').size());
    猜你喜欢
    • 2011-04-14
    • 2011-04-05
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多