我正在编写一个网页,我希望它执行的任务之一是当用户单击相应行的单选按钮时,在文本框中显示表中提供的信息(其数据由 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 个 parent 。
关于javascript - 单击相应的单选按钮时获取 html 表格行信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443067/