Javascript 一个ajax函数在同一表单上为多行运行
我有一个ajax函数,可以根据所选的项目填充多个字段。但我希望有额外的行,人们可以选择更多的项目在相同的形式。并尝试在其余行中运行相同的ajax函数,而不是仅在一行中运行。表单提交后,某些行可能会留空 根据我的代码填充多行中的多个字段的最佳方法是什么 这是我的表单代码,现在只有一行供用户选择和插入,但我希望有大约10行供用户选择和插入:Javascript 一个ajax函数在同一表单上为多行运行,javascript,php,ajax,Javascript,Php,Ajax,我有一个ajax函数,可以根据所选的项目填充多个字段。但我希望有额外的行,人们可以选择更多的项目在相同的形式。并尝试在其余行中运行相同的ajax函数,而不是仅在一行中运行。表单提交后,某些行可能会留空 根据我的代码填充多行中的多个字段的最佳方法是什么 这是我的表单代码,现在只有一行供用户选择和插入,但我希望有大约10行供用户选择和插入: <form name="form" method="post" action="placedOrder.php"> <table wi
<form name="form" method="post" action="placedOrder.php">
<table width="70%" border="5" align="center">
<tr>
<th scope="row">Item Name</th>
<th scope="row">Item SKU</th>
<th scope="row">Quantity</th>
<th scope="row">Side Mark</th>
<th scope="row">Unit Price</th>
<th scope="row">Total Price</th>
</tr>
<tr>
<th scope="row">
<?php
include('connect.php');
$result = mysqli_query("SELECT description FROM products")
or die(mysqli_error());
print '<select name="description" id="description" value="description">';
print '<option value="" disabled selected>Please Select A Product</option>';
while ($info = mysqli_fetch_array($result))
{
$p = $info["description"];
$p = htmlspecialchars($p);
printf('<option value="%s">%s</option>', $p, $p);
}
print '</select>';
?>
</th>
<th scope="row">
<input name="sku_1" id="sku_1" readonly />
</th>
<th scope="row">
<input name="qty_1" />
</th>
<th scope="row">
<input name="note_1" />
</th>
<th scope="row">
<input name="uPrice_1" id="uPrice_1" readonly />
</th>
<th scope="row">
<input name="tPrice_1" id="tPrice_1" readonly />
</th>
</tr>
</table>
<input type="submit"/>
</form>
项目名称
项目SKU
量
侧标志
单价
总价
将AJAX方法声明为接受选择器数组的命名函数:这将允许您重用该函数并添加更多利用该函数的事件处理程序
比如:
function myAJAX(selectorArray) {
$.ajax({
//bunch of stuff
data: { description: $(this).val() },
// more stuff
success: function(data){
var skuId = data[0],
unitPrice = data[1];
selectorArray[0].val(skuId);
selectorArray[1].val(unitPrice);
}
});
}
另一种方法是为事件处理程序使用类选择器,并在其中指定目标元素
$('mySelector').on('change', '#targetSelector', myAJAX(selectors));
<div id="box">
<p id="text">Here is some text.</p>
<p>This is also some text</p>
</div>
这里的想法是使用父选择器和其中的附加选择器
$('mySelector').on('change', '#targetSelector', myAJAX(selectors));
<div id="box">
<p id="text">Here is some text.</p>
<p>This is also some text</p>
</div>
我们选择要异步更改的元素的父元素,并通过AJAX调用过滤到要更改的特定元素。有关更多信息,请参阅jQuery,了解如何在
方法上使用来处理事件。hmmm使用类选择器看起来更容易。我不熟悉ajax函数。如果可能的话,你能再详细解释一下吗?非常感谢你的帮助!希望这就是你想要的,。
$('#box').on('change', '#text', myAJAX);