如何获取已检查的单选按钮和剩余的td的值,但不包括未检查的单选按钮(在tr中) [英] How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

查看:74
本文介绍了如何获取已检查的单选按钮和剩余的td的值,但不包括未检查的单选按钮(在tr中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have here a js code to get the first value of td but what i want is to get the value of the checked radio button and the remaining td's value but not include the radio button that has not been checked (in 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>";
                }


推荐答案

你需要检查一下td有输入类型的无线电,然后检查然后得到值,如果它不是无线电得到文本。

You need to check if the td has input type radio and it is checked then get the value else if it is not radio get the text.

$(".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;
});

<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>

这篇关于如何获取已检查的单选按钮和剩余的td的值,但不包括未检查的单选按钮(在tr中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆