在提交主要报价单之前,我有以下代码来插入多个产品和价格。
$(document).ready(function(){
var counter = 2;
$("#addButton").click(function () {
if(counter>10){
alert("Only 10 textboxes allow");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<br>' + '<label>Product '+ counter + ' </label>' +
'<input type="text" name="product" id="product" value="" >' +
'<label>Price '+ counter + ' </label>' +
'<input type="text" name="price" id="price" value="" >'+ '<a href="#" id="save" >Save!</a>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
$("#save").click(function() {
if(!$("#product").val() || !$("#price").val() ){
apprise("You must define a product and price!");
}else{
var product = $("#product").val();
var price = $("#price").val();
var postdata = 'product='+product+'&price='+price;
$.ajax({
type: "POST",
url: "inserter/add_quotation_product.php",
data: postdata,
success: function() {
$('#TextBoxesGroup').html("<div id='message'></div>");
$('#message').html(product+" Saved!");
}
});
return false;
}
主要的html代码:
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label>Product 1 </label>
<input type=text
id="product"
name="product"
placeholder="product to quote"
required/>
<label>Price 1</label>
<input id="price"
name="price"
type=text
placeholder="price of product"
required>
<a href="#" id="save" >Save!</a>
</div>
</div>
问题是单击手动创建的保存按钮效果很好,但单击 jquery 创建的保存按钮似乎完全无效!