javascript - 单击相应的单选按钮时获取 html 表格行信息

标签 javascript html

我正在编写一个网页,我希望它执行的任务之一是当用户单击相应行的单选按钮时,在文本框中显示表中提供的信息(其数据由 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!");
            }
        }
    }

编辑:我将添加我的问题的图片

enter image description here

在上图中,我单击了第一个单选按钮,相应的信息正确地显示在底部。

enter image description here

但是,在这个中,当我单击第二个按钮并且在信息没有更改之前单击第一个按钮时。

最佳答案

为什么不做这样的事情......

$('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/

相关文章:

php - 在 PHP 中处理多个(未)选中的复选框的最简单和最干净的方法

javascript - 推文按钮,用于推文 URL,不带 http ://www

html - 看不见的谷歌验证码

javascript - 连接 1 个字符串和一个 int 值,同时将 id 值赋予在 HTML 中动态生成的 <button> 字段

javascript - Windows Phone WebBrowser - onClick javascript 不起作用

javascript - jQuery 滚动到 anchor 返回 NaN 作为位置哈希

html - 如果CSS规则发生变化,浏览器是否需要刷新

javascript - 具有不同功能的提交按钮

javascript - 如何使用 Array.prototype.filter 过滤对象?

javascript - 我怎样才能导入在javascript中工作?