如何获取已检查的单选按钮和剩余的td的值,但不包括未检查的单选按钮(在tr中)

时间:2017-02-26 11:08:02

标签: javascript php jquery

我这里有一个js代码来获取td的第一个值,但我想要的是什么 是获取已检查的单选按钮的值和剩余的td值,但不包括未检查的单选按钮(在tr中)................ .................................................. .................................................. ...........

"my js"
     $(".hit").click(function(){   
     var str="";var counter=0;

     $.each($("input[name^='flight']:checked").closest("td").siblings("td:first-child"),
              function () {
         if(counter != 0 )
               str +="&";
               str +="id"+(counter+1)+"=" + $(this).text();
               counter++;
              });

    console.log(str);
    //location.href = "guestdetails.php?" + str;
 });

"my php"
foreach ($query1 as $flights) {
                    echo "<tr>";
                    echo "<td>".$flights['id']."</td>";
                    echo "<td>".$flights['depart']."</td>";
                    echo "<td>".$flights['arrive']."</td>";
                    echo "<td>".$flights['airport']."</td>";
                    echo "<td>".$flights['duration']."</td>";
                    echo "<td><label for=lbl3>  <input type='radio' checked id='lbl3' name='flight1[]' class='hit1' value='".$flights['flyonly']."'> PHP ".number_format($flights['flyonly'])."</label></td>";
                    echo "<td><label for=lbl4>  <input type='radio' checked id='lbl4' name='flight1[]' class='hit1' value='".$flights['flybaggage']."'>PHP ".number_format($flights['flybaggage'])."</label></td>";
                    echo "</tr>";
                }

1 个答案:

答案 0 :(得分:1)

你需要检查td是否有输入类型的无线电并检查它然后获取值否则如果它不是无线电获取文本。

&#13;
&#13;
$(".hit").click(function(){   
var $row = $('table').find('tr:first');
var th=[];
$.each($row.find('th'),
function (index) {	
th[index]=$(this).text();
});
 var str="";var counter=0;var headCounter=th.length;
 $.each($("input[name^='flight']:checked").closest("tr").find('td'),
         function (index) {		   
    if($(this).find('input:radio').length && $(this).find('input:radio').is(':checked')){
     if(counter != 0 )
    str +="&";
   str +="id"+(counter+1)+"=" +  $(this).find('input:radio').val();
   counter++;
 // adding Th here  
    str +="&";
   str +="id"+(counter+1)+"=" +  th[index%headCounter];
   counter++;
    }
    if($(this).find('input:radio').length == 0){
     if(counter != 0 )
    str +="&";
	       str +="id"+(counter+1)+"=" + $(this).text();
      counter++;
      }   });
console.log(str);
//location.href = "guestdetails.php?" + str;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><th>Head1</th><th>Head2</th><th>Head3</th><th>Head4</th></tr>
  <tr> 
<td>f</td>
<td>f</td>
 <td><input type="radio" name="flight1[]" value='1'></td> <!-- FIND THIS -->
  <td><input type="radio" name="flight1[]" value='11'></td> <!-- FIND THIS -->
  </tr>
  <tr>
<td>f1</td>
<td>f</td>
<td><input type="radio" name="flight2[]" value='12'></td> <!-- FIND THIS -->
 <td><input type="radio" name="flight2[]" value='112'></td> <!-- FIND THIS -->
  </tr>
  <tr>
<td>f2</td>
<td>f</td>
 <td><input type="radio" name="flight3[]" value='13'></td> <!-- FIND THIS -->
  <td><input type="radio" name="flight3[]" value='113'></td> <!-- FIND THIS -->
  </tr>
</table>
<button type="button" class="hit">hit</button>
&#13;
&#13;
&#13;