2016-07-07 2 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> 

Будет ли возможность иметь идентификатор элемента в А для или петли Еогеасп?

Спасибо заранее,

Кенни

ответ

-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

Смежные вопросы