我在想代码很丑,因为它太长了,我想知道这有一个简写。我需要帮助!ajax代码的速记
在我看来:
<table class="table table-responsive table-bordered table-hover table-striped" id="table">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($products as $key => $value) : ?>
<tr>
<td>
<span id="data-id"><?php echo $value->prod_id; ?></span>
</td>
<td>
<span id="data-prodname-<?php echo $value->prod_id; ?>"><?php echo $value->product_name; ?></span>
</td>
<td>
₱ <span id="data-price-<?php echo $value->prod_id; ?>"><?php echo $value->price; ?></span>
</td>
<td>
<a class="btn btn-primary btn-edit" id="edit-product-<?php echo $value->prod_id; ?>">
<i class="glyphicon glyphicon-pencil"></i> Edit
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
点击编辑按钮后,jQuery将触发。我看到我的jquery它有很长的代码,我只是替换了html代码,而且我认为它有简写。任何人都可以帮助我?先谢谢你。
$(form).ajaxSubmit({
type: 'post',
url: 'Jewelry_controller/edit_product_exe',
dataType: 'json',
data: dataString,
success: function(callback)
{
var a;
var productContainer = '<table class="table table-responsive table-bordered table-hover table-striped">';
productContainer += '<tr>';
productContainer += '<th>ID</th>';
productContainer += '<th>Product Name</th>';
productContainer += '<th>Price</th>';
productContainer += '<th>Action</th>';
productContainer += '</tr>';
for(var i = 0; i < callback.length; i++)
{
a = callback[i];
var ternary = a.status == '1' ? "Active" : "Inactive";
productContainer += '<tr>';
productContainer += '<td><span id="data-id">' + a.id +'</span></td>';
productContainer += '<td><span id="data-prodname-'+ a.id +'">' + a.product_name + '</span></td>';
productContainer += '<td>₱<span id="data-price-'+ a.id +'">' + a.price + '</span></td>';
productContainer += '<td>';
productContainer += '<a class="btn btn-sm btn-primary btn-edit" data-toggle="modal" data-target="#product-modal" id="edit-product-' + a.id + '"><i class="glyphicon glyphicon-pencil"></i> Edit</a>';
productContainer += '</td>';
productContainer += '</tr>';
}
productContainer += '</table>';
$('#table-product').html(productContainer);
}
});
在成功的代码中,它有很长很长的代码。我想把它缩短,任何人都可以帮忙?在此先感谢您 我是AJAX新手。
你有一个示例代码先生? –
没有ajax的例子,对于主模板化的想法https://jsfiddle.net/br3t/rmzndbss/1/ – br3t