我这里有一个 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>";
}
最佳答案
您需要检查 td 是否具有输入类型 radio 并检查它然后获取值,如果不是 radio 则获取文本。
$(".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>
关于javascript - 如何获取选中的单选按钮的值和剩余的 td 但不包括未选中的单选按钮(在 tr 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467702/