Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中单击按钮以在表中添加行_Javascript_Jquery_Html - Fatal编程技术网

在JavaScript中单击按钮以在表中添加行

在JavaScript中单击按钮以在表中添加行,javascript,jquery,html,Javascript,Jquery,Html,我的任务是制作一个带有表格和按钮的页面。当用户按下按钮时,一行被添加到表中。 因此,我的html页面上有一个按钮(Bla-Bla), 和一个表()。我有一个单独的JavaScript文件。我在那份文件中写道: $(function() { $(my_button).click(function(){ tbl = document.getElementById("my_table"); row = tbl.insertRow(0);

我的任务是制作一个带有表格和按钮的页面。当用户按下按钮时,一行被添加到表中。 因此,我的html页面上有一个按钮(
Bla-Bla
), 和一个表(
)。我有一个单独的JavaScript文件。我在那份文件中写道:

$(function() {
    $(my_button).click(function(){            
       tbl = document.getElementById("my_table");
       row = tbl.insertRow(0);      
       var newCell = row.insertCell(0);
       newCell.height=300;
       newCell.innerHTML="Some New Text";
    });
});
但是有一个问题:当我按下按钮时,行被添加了几毫秒,然后它消失了,我再次看到没有新行的表。
问题是什么?我如何解决它?

如果要使用jQuery进行单击事件,那么最好在代码中一致使用它

试一试:

$(“#我的_按钮”)。单击(函数(){
$(“#我的#表”).append('somenewtext');
});

如果要对单击事件使用jQuery,那么最好在代码中始终如一地使用它

试一试:

$(“#我的_按钮”)。单击(函数(){
$(“#我的#表”).append('somenewtext');
});

在没有看到您的标记的情况下,可能是因为系统的注释表明表单正在提交,页面正在刷新

如果是这种情况,简单的解决方案是在处理程序中使用event.preventDefault()

$(function() {
    $(my_button).click(function(){ 
        event.preventDefault();           
        tbl = document.getElementById("my_table");
        row = tbl.insertRow(0);      
        var newCell = row.insertCell(0);
        newCell.height=300;
        newCell.innerHTML="Some New Text";
   });
}))

您可以在


根据其他答案,如果您可以访问jquery,您可以在整个代码中使用它来整理该方法

在没有看到您的标记的情况下,可能是因为系统的评论表明表单正在提交,页面正在刷新

如果是这种情况,简单的解决方案是在处理程序中使用event.preventDefault()

$(function() {
    $(my_button).click(function(){ 
        event.preventDefault();           
        tbl = document.getElementById("my_table");
        row = tbl.insertRow(0);      
        var newCell = row.insertCell(0);
        newCell.height=300;
        newCell.innerHTML="Some New Text";
   });
}))

您可以在

根据其他答案,如果您可以访问jquery,您可以在整个代码中使用它来整理该方法

这里有一个小例子

html代码

<table class="authors-list">
    <tr>
        <td>author's first name</td><td>author's last name</td>
    </tr>
    <tr>
        <td>
            <input type="text" name="first_name" />
        </td>
        <td>
            <input type="text" name="last_name" />
        </td>
    </tr>
</table>
<a href="#" title="" class="add-author">Add Author</a>

作者的名字作者的姓
jquery代码

var counter = 1;
jQuery('a.add-author').click(function(event){
    event.preventDefault();
    counter++;
    var newRow = jQuery('<tr><td><input type="text" name="first_name' +
        counter + '"/></td><td><input type="text" name="last_name' +
        counter + '"/></td></tr>');
    jQuery('table.authors-list').append(newRow);
});
var计数器=1;
jQuery('a.add-author')。单击(函数(事件){
event.preventDefault();
计数器++;
var newRow=jQuery(“”);
jQuery('table.authors list').append(newRow);
});
请参阅此链接以了解如何使用此代码 这里有一个小例子

html代码

<table class="authors-list">
    <tr>
        <td>author's first name</td><td>author's last name</td>
    </tr>
    <tr>
        <td>
            <input type="text" name="first_name" />
        </td>
        <td>
            <input type="text" name="last_name" />
        </td>
    </tr>
</table>
<a href="#" title="" class="add-author">Add Author</a>

作者的名字作者的姓
jquery代码

var counter = 1;
jQuery('a.add-author').click(function(event){
    event.preventDefault();
    counter++;
    var newRow = jQuery('<tr><td><input type="text" name="first_name' +
        counter + '"/></td><td><input type="text" name="last_name' +
        counter + '"/></td></tr>');
    jQuery('table.authors-list').append(newRow);
});
var计数器=1;
jQuery('a.add-author')。单击(函数(事件){
event.preventDefault();
计数器++;
var newRow=jQuery(“”);
jQuery('table.authors list').append(newRow);
});
请参阅此链接以了解如何使用此代码


$(文档).ready(函数(){
$(document).on(“click”,“td.addNewButton”),函数(e){
变量行=$(this.parent().parent().children().index($(this.parent());
var html=$('#myTable tbody tr:eq('+row+')).html();
$(“#myTable tbody tr:eq(“+row+”))。之后(“+html+”);
}); 
}); 
这将对您有好处:)


$(文档).ready(函数(){
$(document).on(“click”,“td.addNewButton”),函数(e){
变量行=$(this.parent().parent().children().index($(this.parent());
var html=$('#myTable tbody tr:eq('+row+')).html();
$(“#myTable tbody tr:eq(“+row+”))。之后(“+html+”);
}); 
}); 

这对您来说很好:)

页面上是否有其他使用
setTimeout
或类似内容的代码?是否有其他可能与此冲突的Javascript?就其本身而言,它似乎能按预期工作()可能与您的问题无关,但为什么不使用
$(“#我的#表”)
$(“#我的#表”)。append()
?您是否尝试过在单击按钮时提交
表单
,因此页面刷新。页面上是否有其他使用
setTimeout
或类似内容的代码?是否有其他可能与此冲突的Javascript?就其本身而言,它似乎能按预期工作()可能与您的问题无关,但为什么不使用
$(“#我的#表”)
$(“#我的#表”)。append()
?您是否尝试过,当您单击按钮时,您可能正在提交一份
表单,因此页面会刷新。