2016-07-07 100 views
1

我創建了一系列複選框,我想知道是否可以從中獲取值和id元素。舉例來說,如果我有一個HTML表單以下複選框:我可以從複選框中獲取「id」和「value」元素嗎?

<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元素或foreach循環?

由於提前,

-Kenny

回答

-1
<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> 
0
$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

相關問題