[英]Get html table row information when it's corresponding radiobutton is clicked
我正在写一个网页,我要执行的任务之一是在文本框中显示用户单击其相应行的单选按钮时表中提供的信息(其数据由MySQL中的查询确定)。 我设法使其正常运行,但只能运行一次。 如何使它每次都能工作?
非常感谢!
HTML和PHP代码:
<table style="width=100%;border: 1px solid black" id="tablaListado" cellspacing="10">
<tr style="border-bottom-style:solid;border-width:1px;border-color:#1e1e1e;text-align: -webkit-center;background:#22CECE;font-size: 16px;">
<th style = "border: 1px solid black">Elegir</th>
<th style = "border: 1px solid black">SKU</th>
<th style = "border: 1px solid black">ISBN 13</th>
<th style = "border: 1px solid black">Titulo</th>
<th style = "border: 1px solid black">Proveedor</th>
<th style = "border: 1px solid black">Costo</th>
<th style = "border: 1px solid black">Precio</th>
</tr>
<?php
$result = faltanteCostos();
if(mysql_num_rows($result) >0){
while($registroDD = mysql_fetch_assoc($result)){
echo "<tr style='border: 1px solid black'>
<td style='border: 1px solid black'>
<input type='radio' id='regular' name='optradio' onclick='llenarInfo()'>
</td>
<td class='sku' style='border: 1px solid black'>".$registroDD['art_SKU']."</td>
<td class='isbn13' style='border: 1px solid black'>".$registroDD['art_N13']."</td>
<td class='titulo' style='border: 1px solid black'>".$registroDD['art_titulo']."</td>
<td class='prov' style='border: 1px solid black'>".$registroDD['art_id_proveedor']."</td>
<td class='costo' style='border: 1px solid black'>".$registroDD['art_cost']."</td>
<td class='precio' style='border: 1px solid black'>".$registroDD['art_precio']."</td>
</tr>";
}
}
?>
</table>
JS:
function llenarInfo(){
var d = document.getElementsByName('optradio');
for (var i = 0; i < d.length; i++) {
if (d[i].checked) {
document.getElementById("insku").value = $("td input[name='optradio']:checked").parents().find('.sku').html();
document.getElementById("inisbn").value = $("td input[name='optradio']:checked").parents().find('.isbn13').html();
document.getElementById("intit").value = $("td input[name='optradio']:checked").parents().find('.titulo').html();
document.getElementById("inprov").value = $("td input[name='optradio']:checked").parents().find('.prov').html();
document.getElementById("incost").value = $("td input[name='optradio']:checked").parents().find('.costo').html();
document.getElementById("inprecio").value = $("td input[name='optradio']:checked").parents().find('.precio').html();
console.log("yes!");
}
}
}
编辑:我将添加图片我的问题是
在上面的图片中,我单击了第一个单选按钮,相应的信息正确显示在底部。
但是,在此按钮中,当我单击第二个按钮并且单击第一个按钮时,信息不会更改。
为什么不做这样的事情...
$('input[type="radio"]').on('click', function(){
var value1 = $(this).parent().parent().find('.classYouWant').text();
var value2 = $(this).parent().parent().find('.classYouWant').text();
$('#idOfInputBox1').val(value1);
$('#idOfInputBox2').val(value2);
});
顺便说一句,您不需要行中的.html()
,而只需要.text()
。 我也不会使用.parents()
因为您只需要上两个级别,因此只需链接2个父母即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.