我可以从复选框中获取“id”和“值”元素吗?


Can I get the "id" and "value" elements from a checkbox?

我创建了一系列复选框,我想知道是否可以从中获取值和 id 元素。例如,如果我在 html 表单中有以下复选框:

<html>
<body>
    <form action="formInput.php" method="post">
        <table border="1" width="100%">
        <thead><p style="font-family:Arial" style="font-size:15px">This is my Form.</p></thead>
        <tr>
            <td width="33%" ><input type="hidden" id="item_1" name="interventions[]" value="false" />
            <input type="checkbox" id="item_1" name="interventions[]" value="true" />  Item 1</td>
            <td width="33%"><input type="hidden" id="item_2" name="interventions[]" value="false" />
            <input type="checkbox" id="item_2" name="interventions[]" value="true" />  Item 2</td>
        <td width="33%"><input type="hidden" id="item_3" name="interventions[]" value="false" />
            <input type="checkbox" id="item_3" name="interventions[]" value="true" />  Item 3</td>
        </tr>
    </table>
</body>
<html>

是否可以将 id 元素放在 for 或 foreach 循环中?

提前感谢,

-肯尼

$xml=simplexml_load_string($html);
$result=$xml->xpath('//input[@type="checkbox"]');
for ($el in $result) {
    $id=(string)$el->attributes()['id'];
    $value=(string)$el->attributes()['value'];
    $checkboxes[$id]=$value;
}

http://php.net/manual/en/book.simplexml.php

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">   </script>
<script>
    //click checkbox event
    $(':checkbox').click(function(){
        alert($(this).attr('id'));
    });
    //or like this
    var ids = [];
    var values = [];
    $(':checkbox').each(function(i,e){
        ids.push($(this).attr('id'));
        values.push($(this).val());
    });
    console.log('ids:'+ids);  //ids:item_1,item_2,item_3
    console.log('values:'+values); //values:true,true,true
</script>