简体   繁体   English

HTML 输入字段内容不断消失

[英]HTML input fields contents keep disappearing

I am building a webpage to monitor my stocks and mark some comments for each stock.我正在建立一个网页来监控我的股票并为每只股票标记一些评论。 I got the volume and the price of each of the stocks from an api I built and wrote some code to put them into a table.我从我构建的 api 中获得了每只股票的数量和价格,并编写了一些代码将它们放入表格中。 The table has some other fields as well:该表还有一些其他字段:

Name, Price, Volume, Comments, Target Price名称、价格、成交量、评论、目标价格

The comments and target prices contain input fields which I can type in some notes for each stock.评论和目标价格包含输入字段,我可以为每只股票输入一些注释。 However, my code builds a new table every 10s to update the stock prices and volume, and after each sort action which is triggered by pressing the corresponding heading I want to sort by.但是,我的代码每 10 秒构建一个新表来更新股票价格和交易量,并且在通过按下我想要排序的相应标题触发的每个排序操作之后。 After this, my inputs fields contents will disappear.在此之后,我的输入字段内容将消失。 Is there a way to keep these contents intact after an update or a sorting is triggered?有没有办法在触发更新或排序后保持这些内容完好无损?

<script
              src="https://code.jquery.com/jquery-3.6.0.min.js"
              integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
              crossorigin="anonymous"></script>

<table class="table table-striped" style="float: left; width: 60%">
    <tr  class="bg-primary">
        <th data-column="name" data-order="dsc">Name &#9650</th>
        <th data-column="price" data-order="dsc">price &#9650</th>
        <th data-column="volume" data-order="dsc">Volume &#9650</th>
        <th data-column="targetPrice" data-order="dsc">Target Price &#9650</th>
        <th data-column="comments" data-order="dsc">Comments &#9650</th>
        <th data-column="submit" data-order="dsc">Send &#9650</th>
    </tr>

    <tbody id="myTable">
        
    </tbody>
</table>

<script>

var myArray = []
var sortOrder = 'name'
var sortColumn = 'dsc'

setInterval(function() {
    $.ajax({
      method:'GET',
      url:'http://127.0.0.1:5000',
      success:function(response){
        myArray = response.data
        if(sortOrder == 'dsc'){
            myArray = myArray.sort((a,b) => a[sortColumn] > b[sortColumn] ? 1 : -1)

        }else{
            myArray = myArray.sort((a,b) => a[sortColumn] < b[sortColumn] ? 1 : -1)
        }
        buildTable(searchTable(myArray))
      }
    })
}, 10000)

function buildTable(data){
    var table = document.getElementById('myTable')

    table.innerHTML = "" 
    for (var i = 0; i < data.length; i++){
      var row = `<tr>
              <td>${data[i].name}</td>
              <td>${data[i].price}</td>
              <td>${data[i].volume}</td>
              <td><input type="number" name="${data[i].symbol}_targetPrice" style='width:60px;'></td>
              <td><input type="number" name="${data[i].symbol}_comments" style='width:60px;'></td>
              <td><input type="submit"></td>
            </tr>`
      table.innerHTML += row

    }
  }

</script>

The submit button is to send back the name, target price and comments as a dictionary back to the server, which I'm still figuring out how to do, however, the problem of the field contents disappearing is a much larger problem.提交按钮是将名称,目标价格和评论作为字典发送回服务器,我仍在弄清楚如何做,但是字段内容消失的问题是一个更大的问题。 Thanks in advance.提前致谢。

I think that your problem is in the UX more than in the code.我认为您的问题出在用户体验中,而不是在代码中。

Re-building the HTML containing input elements every 10 seconds is a bad idea because:每 10 秒重新构建包含输入元素的 HTML 是一个坏主意,因为:

  • You're going to lose focus of the input that you're typing in.您将失去对正在输入的输入的关注。
  • It's going to be overridden with the new data.它将被新数据覆盖。 (the problem you're facing) (你面临的问题)
  • It's confusing that inputs that you don't change get updated automatically.令人困惑的是,您不更改的输入会自动更新。

To me it makes more sense to have a table with all the data (but no inputs), that can be updated every 10s.对我来说,拥有一个包含所有数据(但没有输入)的表更有意义,它可以每 10 秒更新一次。 And an edit button that when is clicked shows a form with the inputs that won't update while you're typing.单击时的编辑按钮会显示一个表单,其中的输入在您键入时不会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM