问题1 =我有以下脚本:
$(document).ready(function(){
$(".form-inline").on("submit",function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "php/additem.php",
data: {
itemName: $("#itemName").val(),
pricetotal: $("#price").val(),
description: $("#description").val(),
qty: $("qtyitem"]").val()
},
success: function(data)
{
alert(data);
}
});
});
});
但是这不起作用,因为此脚本必须附加的项目在发生特定的ajax请求之前不会显示。所以我需要一个能够正确附加的方法,这是我尝试过的(这是在我的php文件中):
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$price ="";
if ($_SESSION['customer_band'] == 'A') {
$price = $row['bandA'];
}
else if ($_SESSION['customer_band'] == 'B') {
$price = $row['bandB'];
}
else if ($_SESSION['customer_band'] == 'C') {
$price = $row['bandC'];
}
else if ($_SESSION['customer_band'] == 'D') {
$price = $row['bandD'];
}
else if ($_SESSION['customer_band'] == 'E') {
$price = $row['bandE'];
}
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
<div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
<div class="content-boxes-text">
<form action="php/additem.php" method="post" class="form-inline pull-right">
<h4>'.$row['itemName'].'</h4><input id="itemName" type="hidden" name="itemName" value="'.$row['itemName'].'">
<h3>$'.$price.'</h3><input id="price" type="hidden" name="pricetotal" value="'.$price.'">
<img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
<p>'.$row['description'].'</p><input id="description" type="hidden" name="description" value="'.$row['description'].'">
<div class="form-group">
<label class="sr-only" for="qtyitem">Qty</label>
<div class="input-group">
<input type="number" name="qty" class="form-control" id="qtyitem" placeholder="How Many?">
</div>
</div>
<button type="submit" id="additem" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
<!-- //.content-boxes -->
</div>
<script type="text/javascript">
$(document).ready(function(){
$(".form-inline").on("submit",function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "php/additem.php",
data: {
itemName: $("#itemName").val(),
pricetotal: $("#price").val(),
description: $("#description").val(),
qty: $("#qtyitem").val()
},
success: function(data)
{
alert(data);
}
});
});
});
</script>
';
}
}
即使这样可行,但这会运行很多次。我需要将此代码置于此while语句之外。
我的第二个问题是QTY没有任何警报。当我在没有ajax的情况下执行此查询时,QTY正常工作。所以我假设某些内容与我如何在ajax脚本中选择输入有关?
谢谢大家