以下是使用 jQuery 和 AJAX 使用 HTML5 表資料到資料庫表的行內插入。我還想連同這個表格資料一起發送一個復選框資料,我該如何實作呢?
<div id="add-product">
<div class="txt-heading">Add Product</div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Description</strong></th>
<th style="text-align:right;"><strong>Price</strong></th>
</tr>
<tr><form name="myform"><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /></form>
<td contentEditable="true" data-id="product_name"></td>
<td contentEditable="true" data-id="product_code"></td>
<td contentEditable="true" data-id="product_desc"></td>
<td contentEditable="true" data-id="product_price" style="text-align:right;"></td>
</tr>
</tbody>
</table>
<div id="btnSaveAction">Save to Database</div>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$("#btnSaveAction").on("click",function(){
params = ""
$("td[contentEditable='true']").each(function(){
if($(this).text() != "") {
if(params != "") {
params = "&";
}
params = $(this).data('id') "=" $(this).text();
}
});
if(params!="") {
$.ajax({
url: "insert-row.html",
type: "POST",
data:params,
success: function(response){
$("#ajax-response").append(response);
$("td[contentEditable='true']").text("");
}
});
}
});
</script>
代碼來自
uj5u.com熱心網友回復:
解決方案可能是這樣的......如果我理解正確你的要求。
<div id="add-product">
<div class="txt-heading">Add Product</div>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Description</strong></th>
<th style="text-align:right;"><strong>Price</strong></th>
</tr>
<tr><form name="myform"><input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /></form>
<td contentEditable="true" data-id="product_name"></td>
<td contentEditable="true" data-id="product_code"></td>
<td contentEditable="true" data-id="product_desc"></td>
<td contentEditable="true" data-id="product_price" style="text-align:right;"></td>
<td><input type="checkbox" id="product_xxxx" name="product_xxxx" value="yes"></td>
</tr>
</tbody>
</table>
<div id="btnSaveAction">Save to Database</div>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$("#btnSaveAction").on("click",function(){
params = ""
$("td[contentEditable='true']").each(function(){
if($(this).text() != "") {
if(params != "") {
params = "&";
}
params = $(this).data('id') "=" $(this).text();
}
});
// For each checkbox whose name begins with 'product_'
$("[type=checkbox][name^='product_']").each(function(){
// If Checkbox Checked
if($(this).prop('checked')) params = $(this).attr('id') "=" $(this).val();
});
// End - For each checkbox whose name begins with 'product_'
if(params!="") {
$.ajax({
url: "insert-row.html",
type: "POST",
data:params,
success: function(response){
$("#ajax-response").append(response);
$("td[contentEditable='true']").text("");
}
});
}
});
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/503859.html
標籤:javascript php jQuery 阿贾克斯
