2014-10-27 106 views
0

所以我有这个功能,当我点击添加按钮时我不希望页面刷新。你可以帮我吗?ASP.NET - 不想页面刷新

<div class="form-group"> 
      <label for="autores">Autores</label> 
      <textarea runat="server" class="form-control" rows="1" id="autores"></textarea> 
      <asp:Button runat="server" OnClientClick="addAutor();" Text="Add" CssClass="btn btn-default" AutoPostback=false /> 
     </div> 
    </div> 
    <div class="col-md-4"> 
     <table id="lista" contenteditable> 
      <tr> 
       <td><strong>Autores</strong></td> 
      </tr> 
     </table> 
     <script> 
      function addAutor() { 
       var table = document.getElementById("lista"); 
       var row = table.insertRow(1); 
       var cell1 = row.insertCell(0); 
       var cell2 = row.insertCell(1); 
       cell1.innerHTML = "NEW CELL1"; 
       cell2.innerHTML = "NEW CELL2"; 
       return false; 
      } 
     </script> 
    </div> 

行!所以...我也试着这样做:

<table id="myTable"> 
      <tr> 
       <td>Row1 cell1</td> 
       <td>Row1 cell2</td> 
      </tr> 
      <tr> 
       <td>Row2 cell1</td> 
       <td>Row2 cell2</td> 
      </tr> 
      <tr> 
       <td>Row3 cell1</td> 
       <td>Row3 cell2</td> 
      </tr> 
     </table> 
     <br> 

     <button onclick="myFunction()">Try it</button> 

     <script> 
      function myFunction() { 
       var table = document.getElementById("myTable"); 
       var row = table.insertRow(0); 
       var cell1 = row.insertCell(0); 
       var cell2 = row.insertCell(1); 
       cell1.innerHTML = "NEW CELL1"; 
       cell2.innerHTML = "NEW CELL2"; 
      } 
     </script> 

与HTML按钮...但它仍然无法正常工作,因为它增加了内容的表,但页面重新加载,而新的内容在前看不见。

+0

'OnClientClick =“return addAutor();”'应该已经有所作为 – DoXicK 2014-10-27 16:18:06

+0

为什么你使用asp按钮如果你不想发布回到服务器?这是有点asp ...为什么不只是使用一个简单的HTML按钮?另外 - 如果你仍然有麻烦 - 请看javascript中的event.preventDefault()。 – 2014-10-27 16:21:25

+0

@doxick我想让我的代码做的是向表中添加一个新行。但是当页面重新加载时,表格的新内容消失了。我希望在接口上发生这种情况,而不是打电话给服务器 – StackJP 2014-10-29 16:57:22

回答

1
<asp:button id="Button1" 
     text="Submit" 
     onclick="addAutor();" 
     usesubmitbehavior="false" 
     runat="server"/> 

设置usesubmitbehavior假更改按钮从提交按钮类别

我看你的样品做工精细,可添加类型= “按钮”

  
 
      function myFunction() { 
 
       var table = document.getElementById("myTable"); 
 
       var row = table.insertRow(0); 
 
       var cell1 = row.insertCell(0); 
 
       var cell2 = row.insertCell(1); 
 
       cell1.innerHTML = "NEW CELL1"; 
 
       cell2.innerHTML = "NEW CELL2"; 
 
      } 
 
    
<table id="myTable"> 
 
      <tr> 
 
       <td>Row1 cell1</td> 
 
       <td>Row1 cell2</td> 
 
      </tr> 
 
      <tr> 
 
       <td>Row2 cell1</td> 
 
       <td>Row2 cell2</td> 
 
      </tr> 
 
      <tr> 
 
       <td>Row3 cell1</td> 
 
       <td>Row3 cell2</td> 
 
      </tr> 
 
     </table> 
 
     <br> 
 

 
     <button onclick="myFunction()">Try it</button>

+0

,这是行不通的。 – StackJP 2014-10-29 16:55:13

+0

我想让我的代码做的是向表中添加一个新行。但是当页面重新加载时,表格的新内容消失了。 我希望在接口上发生这种情况,而不是打电话给服务器。 – StackJP 2014-10-29 16:56:41

+0

你的第二个示例工作正常,因为我在我的答案中添加了 – 2014-10-29 19:36:33