最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - refresh html table with onclick - Stack Overflow

programmeradmin1浏览0评论

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

<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>

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

<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>
Share Improve this question asked Jun 20, 2014 at 14:22 cassocasso 3291 gold badge2 silver badges9 bronze badges 5
  • 1 Refresh from what? Are you receiving new data from somewhere? – tymeJV Commented Jun 20, 2014 at 14:23
  • Nothing es out because your function doesn't do anything. What do you expect it to do? – Jonathan Commented Jun 20, 2014 at 14:25
  • document.getElementById('mytable').innerHTML do nothing – hindmost Commented Jun 20, 2014 at 14:25
  • This could only work if your random number was dynamic, i.e. was created by a function, rather than user inputted. At the moment it just says "random_number". – Sam Denton Commented Jun 20, 2014 at 14:28
  • As you have it, clicking the table just takes all the data from the table, then stores it temporarily, nothing else. – Sam Denton Commented Jun 20, 2014 at 14:31
Add a ment  | 

2 Answers 2

Reset to default 1

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

 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:-

<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

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.

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

发布评论

评论列表(0)

  1. 暂无评论