javascript 使用 onclick 刷新 html 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24329486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:34:48  来源:igfitidea点击:

refresh html table with onclick

javascripthtmlonclick

提问by casso

I'm trying to refresh my table with onclick button, but nothing really comes out. Can't figure out what I do wrong here. Any ideas?

我正在尝试使用 onclick 按钮刷新我的表格,但没有真正出现。无法弄清楚我在这里做错了什么。有任何想法吗?

<div id="mytable">   
        <table>
             <tr>
                <td>{{ random_number }}</td>
                <td>{{ random_number }}</td>
                <td>{{ random_number }}</td>
            </tr>
        </table> 
    </div>
    <script>
    function Reload()   {
        document.getElementById('mytable').innerHTML;
    }
    </script>
    <button onclick="Reload()">Refresh table</button>

回答by Mustafa sabir

Its not clear What you are trying to refresh, But assuming you are trying to generate and refresh random numbers in the table, yu can try this:-

不清楚您要刷新什么,但是假设您要尝试生成和刷新表中的随机数,您可以尝试以下操作:-

JS

JS

 function Reload()   {

     document.getElementById('td1').innerHTML=GenRand();
     document.getElementById('td2').innerHTML=GenRand();
     document.getElementById('td3').innerHTML=GenRand();
    }
  function GenRand(){
     return Math.floor((Math.random() * 10) + 1);//generates random number between 1 and 10 , you can edit values to generate any range.
    } 

HTML:-

HTML:-

<div >   
    <table  border="1">
         <tr >
           <td id="td1">1</td>
           <td id="td2">2</td>
           <td id="td3">3</td>
          </tr>
     </table> 
 </div>
 <button onclick="Reload()">Refresh table</button>

Fiddle

小提琴

回答by Samuel Dauzon

If you use a server-side framework (I think {{ random_number }} tag is used by a template/views systems) you must to get data from server.

如果您使用服务器端框架(我认为 {{ random_number }} 标记由模板/视图系统使用),您必须从服务器获取数据。

You can use AJAX to get data or you can refresh all the page.

您可以使用 AJAX 获取数据,也可以刷新所有页面。