javascript-使用按钮在php中动态添加新行到表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-使用按钮在php中动态添加新行到表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3854字,纯文字阅读大概需要6分钟。
内容图文
![javascript-使用按钮在php中动态添加新行到表](/upload/InfoBanner/zyjiaocheng/667/65369dc4fd3943c5ba1e95c3221df7c8.jpg)
我已经使用php创建了动态表格.现在我想添加此功能.
当我们单击提交按钮时,它将自动将数据添加到数据库并创建新行,并且应禁用上一行以使其无法编辑.如何执行此操作?
<?php
session_start();
require_once "header.php";
?>
<body>
<div class="text-center">
<h1>PAYMENT PAGE</h1>
</div>
<hr>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
User ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
NIC
</th>
<th class="text-center">
Amount
</th>
<th class="text-center">
Date
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='uid' placeholder='User ID' class="form-control"/>
</td>
<td>
<input type="text" name='uname' placeholder='Name' class="form-control"/>
</td>
<td>
<input type="text" name='nic' placeholder='NIC' class="form-control"/>
</td>
<td>
<input type="text" name='amount' placeholder='Amount' class="form-control"/>
</td>
<td>
<input type="date" name='dt' placeholder='Date' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
</div>
</div>
<button id="add_row" class="btn btn-primary btn-lg pull-left" >SUBMIT</button>
</div>
</body>
</html>
$(document).ready(function(){
var i=1;
$("#add_row").click(function(){
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><input type='text' name='uid"+i+"' placeholder='User ID' class='form-control input-md'/></td><td><input type='text' name='uname"+i+"' placeholder='Name' class='form-control input-md'/></td><td><input type='text' name='nic"+i+"' placeholder='NIC' class='form-control input-md'/></td><td><input type='text' name='amount"+i+"' placeholder='Amount' class='form-control input-md'/></td><td><input type='date' name='dt"+i+"' placeholder='Date' class='form-control input-md'/></td>");
$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
});
解决方法:
尝试在添加新行之前简单地使用$(‘tr’).find(‘input’).prop(‘disabled’,true)禁用所有先前的行输入.
Jquery Documentation of prop()
更新了jQuery版本3.2.1
$(document).ready(function() {
var i = 1;
$("#add_row").click(function() {
$('tr').find('input').prop('disabled',true)
$('#addr' + i).html("<td>" + (i + 1) + "</td><td><input type='text' name='uid" + i + "' placeholder='User ID' class='form-control input-md'/></td><td><input type='text' name='uname" + i + "' placeholder='Name' class='form-control input-md'/></td><td><input type='text' name='nic" + i + "' placeholder='NIC' class='form-control input-md'/></td><td><input type='text' name='amount" + i + "' placeholder='Amount' class='form-control input-md'/></td><td><input type='date' name='dt" + i + "' placeholder='Date' class='form-control input-md'/></td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
i++;
});
});
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<div class="text-center">
<h1>PAYMENT PAGE</h1>
</div>
<hr>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th class="text-center">
#
</th>
<th class="text-center">
User ID
</th>
<th class="text-center">
Name
</th>
<th class="text-center">
NIC
</th>
<th class="text-center">
Amount
</th>
<th class="text-center">
Date
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='uid' placeholder='User ID' class="form-control" />
</td>
<td>
<input type="text" name='uname' placeholder='Name' class="form-control" />
</td>
<td>
<input type="text" name='nic' placeholder='NIC' class="form-control" />
</td>
<td>
<input type="text" name='amount' placeholder='Amount' class="form-control" />
</td>
<td>
<input type="date" name='dt' placeholder='Date' class="form-control" />
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
</div>
</div>
<button id="add_row" class="btn btn-primary btn-lg pull-left">SUBMIT</button>
</div>
内容总结
以上是互联网集市为您收集整理的javascript-使用按钮在php中动态添加新行到表全部内容,希望文章能够帮你解决javascript-使用按钮在php中动态添加新行到表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。