【问题标题】:Get onclick values from HTML table to input form JavaScript从 HTML 表中获取 onclick 值以输入表单 JavaScript
【发布时间】:2018-09-25 21:42:46
【问题描述】:

我有一个 HTML 表格,它通过 AJAX 每 1 秒更新一次。 AJAX 从外部 PHP 文件中获取数据并回显它。这是 HTML 表格:

<div style='float: left;'>
<br><br>
<p style="padding-left:16px; font-size: 20px;">Amount(<?php echo $market; ?>) | Price(<?php echo $bm; ?>) &nbsp | Total(<?php echo $bm; ?>)</p>
<div class="panel-hello scrollbar" id="style-11">
    <div class="data-table">
        <table class="table table-hello table-bordered table-hover force-overflow" id="btcaddresses">
            <tbody style="border: 1px solid green; height: 300px; overflow-y: scroll;">

            </tbody>
        </table>
    </div>
</div>
</div>

现在我有一个包含三个输入的表单。 html 表提供了 3 列的行。这是表格:

  <form name="yourform" method="post" action="btccpa.php" style="float:right;">
    <div id="log_err">
        <strong><?php if(isset($eroor)) { echo $eroor; } ?><?php if($au == 1) { echo 'Please log in to trade.';} ?></strong> 
    </div>  
    <p>
        <label style="float: left;">Price:</label>
        <input class="input101" style="float: left;" type="text" name="uprice" id="box3" oninput="calculate()">
        <label style="float: right;">: <?php echo $market; ?></label>
        <input class="input101" style="float: right;" type="text" name="uam" id="box4" oninput="calculate()"><br>
        <input class="input101" style="float: right;" type="text" name="utam" id="resul"><br>
        <label style="padding: 10px;">Total <?php echo $bm; ?>:</label>
        <td rowspan="2">
            <input type="hidden" name="yourform" value="1">
            <br><br>
            <span class="orderbutton" type="submit" id="ordersell" onclick="yourform.submit()">SELL</span>
        </td>
    </p>
</form>

我希望 html 表中的值在单击时传递给表单。这是正在使用的 AJAX:

    function loadXMLD()
 {
 var xmlhttp;
 if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("btcaddresses").innerHTML=xmlhttp.responseText; // your div
  }
}
  xmlhttp.open("GET","get/sellbtccpa.php",true); //your php file
 xmlhttp.send();
}
window.setInterval(function(){
 loadXMLD();
 }, 1000);

【问题讨论】:

  • "// IE6、IE5 的代码" — 哇!现在是 2018 年!
  • 如果表格每1秒更新一次,一旦你点击它并且你填写表格数据就会过时
  • @LelioFaieta 没关系,但怎么做!
  • 没有答案?我以为有人会知道

标签: javascript php ajax html


【解决方案1】:
        var table = document.getElementById("btcaddresses");
   var j;
    if (table != null) {
        for (var i = 0; i < table.rows.length; i++) {
            for (j = 0; j < table.rows[i].cells.length; j++)

                table.rows[i].cells[j].onclick = function () {
                  var rownumber;
                  rownumber = $(this).closest("tr").index();
                  document.getElementById("box3").value= table.rows[rownumber].cells[0].innerHTML;
                   document.getElementById("box4").value= table.rows[rownumber].cells[1].innerHTML;
                   document.getElementById("resul").value= table.rows[rownumber].cells[2].innerHTML;


}}}

试试这个。 box3、box4、resul 是您的输入 ID。顺便说一句,您需要 JQuery。

【讨论】:

  • 行数是多少[行号]
  • 点击单元格的行数
猜你喜欢
  • 2019-02-26
  • 2013-02-28
  • 1970-01-01
  • 2017-02-21
  • 2018-02-18
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多