gpt4 book ai didi

javascript - 动态 php-ajax 表单

转载 作者:可可西里 更新时间:2023-11-01 07:37:13 25 4
gpt4 key购买 nike

我对 ajax 不是很了解,所以我无意中发现了这一点。我有一个带有添加/删除行的动态表单。在这种形式中,我希望在不刷新页面的情况下有一个“可靠的下拉菜单”,所以最简单的方法是使用 JS。下拉列表似乎可以很好地处理一行数据,但是当我添加另一行时它就不行了。有没有办法修改某些内容以便在无限行数的情况下正常运行?

<script>
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 30){
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Numarul maxim de repere este 30.");

}
}

function deleteRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) { // limit the user from removing all the fields
alert("Nu se pot sterge toate reperele.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}
</script>



<script>
function showCateg(str)
{
if (str=="")
{
document.getElementById("showcateg").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showcateg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcateg.php?brand="+str,true);
xmlhttp.send();
}
</script>



<input type="button" class="btn_rosu" onClick="addRow('dataTable')" value="Adauga" />
<input type="button" class="btn_rosu" onClick="deleteRow('dataTable')" value="Elimina" />

<p>(Se elimina numai randurile bifate)</p>

<table id="dataTable" class="bg" border="0">
<tbody>
<tr>
<p>
<td><input name="chk[]" type="checkbox" required class="btn_rosu" checked="checked" /></td>
<td><label>Brand</label>
<select name="BX_BRAND[]" class="btn_gri" required="required" onChange="showCateg(this.value)">

<?php
do {
?>
<option value="<?php echo $row_brand['brand']?>"><?php echo $row_brand['brand']?></option>

<?php
} while ($row_brand = mysql_fetch_assoc($brand));
$rows = mysql_num_rows($brand);
if($rows > 0) {
mysql_data_seek($brand, 0);
$row_brand = mysql_fetch_assoc($brand);
}
?>
</select></td>
<td>
<label for="BX_CATEG">Categ.</label>
<div id="showcateg"></div>
</td>
<td>
<label for="BX_gender">Reper</label>
<select name="BX_REPER[]" class="btn_gri" id="BX_REPER" required="required">

<?php
do {
?>

<option value="<?php echo $row_reper['reper']?>"><?php echo $row_reper['reper']?></option>

<?php
} while ($row_reper = mysql_fetch_assoc($reper));
$rows = mysql_num_rows($reper);
if($rows > 0) {
mysql_data_seek($reper, 0);
$row_reper = mysql_fetch_assoc($reper);
}
?>
</select>
</td>
<td>
<label for="BX_birth">Pret</label>
<input name="BX_PRET[]" type="text" required class="btn_gri" id="BX_PRET" size="5" /></td>
<td>Promo
<label for="select"></label>
<select name="BX_PROMO[]" class="btn_gri" id="select">
<option value="1">Da</option>
<option value="2">Nu</option>
</select></td>
</tr>
</tbody>
</table>

谢谢你:)

最佳答案

在我的脑海中,您有多种选择来执行此操作。

简单的方法

将下拉列表放入一个名为 <div> 的列表中:

[...]
<td><div id="dd_cell"><label>Brand</label>
<select name="BX_BRAND[]" class="btn_gri" required="required" onChange="showCateg(this.value)">

<?php
do {
?>
<option value="<?php echo $row_brand['brand']?>"><?php echo $row_brand['brand']?></option>

<?php
} while ($row_brand = mysql_fetch_assoc($brand));
$rows = mysql_num_rows($brand);
if($rows > 0) {
mysql_data_seek($brand, 0);
$row_brand = mysql_fetch_assoc($brand);
}
?>
</select></div>
</td>
[...]

然后在addRow :

[...]
var firstCell = row.insertCell(0);
firstCell.innerHTML = $( "#dd_cell" ).clone();
for(var i=1; i<colCount; i++) { // <-- PAY ATTENTION TO THE "1"!
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
[...]

更难、稍微复杂(但可能更正确)的方法

使下拉列表成为 javascript 函数:

function makeDropDown() {
var dd = "<select name=\"BX_BRAND[]\" class=\"btn_gri\" required=\"required\" onChange=\"showCateg(this.value)\">";
<?php
$rows = mysql_num_rows($brand);
if($rows > 0) {
mysql_data_seek($brand, 0);
while ($row_brand = mysql_fetch_assoc($brand)) {
?>
dd += "<option value='\"<?php echo $row_brand['brand']?>'\"><?php echo $row_brand['brand']?></option>";
<?php
}
}
?>
dd += "</select>";
return dd;
}

然后替换(在表格部分):

[...]
<td><div id="dd_cell"><label>Brand</label>
<script type="text/javascript">
document.write(makeDropDown());
</script></div>
</td>
<td>
<label for="BX_CATEG">Categ.</label>
[...]

最后在函数中 addRow(tableID) :

[...]
var newcell = row.insertCell(0);
newcell.innerHTML = makeDropDown();
for(var i=1; i<colCount; i++) {
newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
[...]

关于javascript - 动态 php-ajax 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21740958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com