我有一个带有带有动态字段和动态行的表的表格。因此,最初,该表包含一行包含两个字段,当我同时填写它们并单击Enter时,jQuery函数将在下面创建新行。
[提交表单的时间到了,我正在检查是否有空字段,如果有任何空字段会显示错误,但是问题出在这里,它将清除所有字段。提交时如何仍保留它们?
他是HTML:
<form action="" method="post">
<button type="submit" class="btn" name="add_device">Add devices</button>
<?php foreach ($dev_type_results as $row) {
} ?>
<table id="ble_table" class="table table-bordered table-striped" cellspacing="0" width="100%">
<tr>
<th>No.</th>
<th>Serial no.</th>
<th>IMEI</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" class="inputs" name="serial_no[]" value="<?php if (isset($_POST['serial_no'])) { echo htmlspecialchars($_POST['serial_no']);} ?>" /></td>
<td><input type="text" class="inputs lst" name="imei[]" value="<?php if (isset($_POST['imei'])) { echo htmlspecialchars($_POST['imei']); } ?>" /></td>
</tr>
</table>
</form>
jquery函数:
var i = $('table tr').length;
$(document).on('keyup', '.lst', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
html = '<tr>';
html += '<td>' + i + '</td>';
<?php foreach ($dev_type_results as $row) {
}
html += '<td><input type="text" class="inputs" name="serial_no[]' + i + '" value="<?php if (isset($_POST['serial_no'])) { echo htmlspecialchars($_POST['serial_no']);} ?>"/></td>';
html += '<td><input type="text" class="inputs lst" name="imei[]' + i + '" value="<?php if (isset($_POST['imei'])) { echo htmlspecialchars($_POST['imei']);} ?>"/></td>';
html += '</tr>';
$('table').append(html);
$(this).focus().select();
i++;
}
});
$(document).on('keydown', '.inputs', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
event.preventDefault();
}
});
为简洁起见,我将在经过验证的地方发布PHP代码:
if (count(array_filter($_POST['serial_no'])) < count($_POST['imei'])) {
array_push($errors, "Found empty field in Serial no. column");
}else{
//call function to post to database
}
我已经尝试过使用全局变量,但是它不起作用。我还能尝试什么?
您可以在输入中添加ids并使用javascript查找并更改其内容。
var Input_1 = document.getElementById(Input_1 - id);
var Input_2 = document.getElementById(Input_2 - id);
var Val_1;
var Val_2;
var completed = false;
var i = $('table tr').length;
$(document).on('keyup', '.lst', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
html = '<tr>';
html += '<td>' + i + '</td>';
html += '<td><input type="text" class="inputs" name="serial_no[]' + i + '" value="<?php if (isset($_POST['
serial_no '])) { echo htmlspecialchars($_POST['
serial_no ']);} ?>
"/></td>';
html += '<td><input type="text" class="inputs lst" name="imei[]' + i + '" value="<?php if (isset($_POST['
imei '])) { echo htmlspecialchars($_POST['
imei ']);} ?>"/></td>';
html += '</tr>';
$('table').append(html);
$(this).focus().select();
i++;
}
});
$(document).on('keydown', '.inputs', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
completed = true
var index = $('.inputs').index(this) + 1;
$('.inputs').eq(index).focus();
event.preventDefault();
Input_1.value = Val_1;
Input_2.value = Val_2;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="post">
<button type="submit" class="btn" name="add_device">Add devices</button>
<?php foreach ($dev_type_results as $row) {
} ?>
<table id="ble_table" class="table table-bordered table-striped" cellspacing="0" width="100%">
<tr>
<th>No.</th>
<th>Serial no.</th>
<th>IMEI</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" class="inputs" name="serial_no[]" value="<?php if (isset($_POST['serial_no'])) { echo htmlspecialchars($_POST['serial_no']);} ?>" /></td>
<td><input type="text" class="inputs lst" name="imei[]" value="<?php if (isset($_POST['imei'])) { echo htmlspecialchars($_POST['imei']); } ?>" /></td>
</tr>
</table>
</form>
<?php
if (count(array_filter($_POST['serial_no'])) < count($_POST['imei'])){
array_push($errors, "Found empty field in Serial no. column");
}else{
//call function to post to database
}
?>