在wordpress中,我有这样的数据库表
product name
id product_name
1 A
2 B
3 C
4 D
product_buying_price
id product_name product_buying_price
1 A 10
2 B 12
3 C 15
4 D 18
product_selling_price
id product_name product_selling_price
1 A 12
2 B 13
3 C 19
4 D 23
所以我有一个这样的下拉列表<select name="product_name" id="product_name">
<option value="">Products</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
我想在这样的表格中显示产品购买价格和产品销售价格的值<table>
<tr>
<td>
<select name="product_name" id="product_name">
<option value="">Products</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
</td>
<td class="product_buying_price"></td>
<td class="product_selling_price"></td>
</tr>
</table>
像这样,我有3-4个这样的字段,我想得到产品的值。在js文件中,我制作了如下ajax
$(\'body\').on(\'change\',\'#product_name\', function() {
var selected = $(this).val;
//Get product buying price
$.post( test.ajaxUrl, { \'selected\' : selected, \'action\' : \'get_product_buying_price\' }, function(data){
data = $.parseJSON(data);
selected.find(\'td.product_buying_price\').html(data);
});
//Get product selling price
$.post( test.ajaxUrl, { \'selected\' : selected, \'action\' : \'get_product_selling_price\' }, function(data){
data = $.parseJSON(data);
selected.find(\'td.product_selling_price\').html(data);
});
});
内部函数我有这样的函数function get_product_buying_price() {
global $wpdb;
$selected_product = $_POST[\'selected\'];
$get_product_price = $wpdb->get_row("SELECT `product_buying_price` FROM `product_buying_price` WHERE `product_name` = \'.$selected_product.\' ");
$product_buying_price = $get_product_price->product_buying_price;
echo json_encode($product_buying_price);
exit;
}
function get_product_selling_price() {
global $wpdb;
$selected_product = $_POST[\'selected\'];
$get_product_price = $wpdb->get_row("SELECT `product_selling_price` FROM `product_selling_price` WHERE `product_name` = \'.$selected_product.\' ");
$product_selling_price = $get_product_price->product_selling_price;
echo json_encode($product_selling_price);
exit;
}
这里很好用。但是,您不认为一次单击就执行多个ajax请求会使速度变慢吗?我有大约3-4个要求更改的请求。有人能告诉我一些更聪明的方法来实现这一点吗?任何帮助和建议都是值得赞赏的。谢谢