我有4张这样的 table
然后我要填写这个表格
工作流程是,当我选择Item Type和Treatment时,价格应该根据ServicePrice Table填写,我可以得到ServicePrice 中的 Price,但是当我在网络上检查 XHR 时,它显示了 Price 的值,但它的数据只是第一次显示,我意味着当模态打开时,它显示第一个数据,当选项更改时,价格 值保持像第一次模态打开一样。所以我想每当 Item Type 或 Treatment 改变时,我想让价格框动态填充
这是我的表格
<tr>
<td>Item Type</td>
<td>
<select name="ItemTypeID" class="form-control" id="ItemType" >
<option selected=""></option>
<?php
$sql2 = mysql_query("select * from itemtype");
while($record = mysql_fetch_array($sql2)){
?>
<option value="<?php echo $record['ID']; ?>" title="<?php echo $record['DefaultSpec']; ?>"> <?php echo $record["Name"]; ?> </option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Treatment</td>
<td>
<select name="TreatmentID" class="form-control" id="Treatment">
<?php
$sql2 = mysql_query("select * from treatment");
while($record = mysql_fetch_array($sql2)){
?>
<option value="<?php echo $record['ID']; ?>"> <?php echo $record["Name"]; ?> </option>
<?php } ?>
</select>
</td>
</tr>
然后是我的ajax
$("#ItemType, #Treatment").change(function(){
var ItemType = $(this).val();
var Treatment = $(this).val();
console.log(Treatment);
console.log(ItemType);
$.ajax({
type: "POST",
dataType: "html",
url: "GetPrice.php",
data: {ItemTypeID: ItemType, TreatmentID: Treatment},
success: function(result){
console.log(result);
$("#Price").val(result);
});
});
我的 GetPrice.php
<?php include "../Content/connection.php";
$a="SELECT * FROM ServicePrice WHERE ItemtypeID = '".$_POST["ItemTypeID"]."' AND TreatmentID = '".$_POST["TreatmentID"]."'";
$q=mysql_query($a);
while($record=mysql_fetch_array($q)){
echo $record['Price'];
}
?>
编辑:
我是这样做的,它给了我正确的答案,但价格值只有在治疗下拉菜单发生变化时才会触发,我怎样才能让它通过展位下拉菜单触发?
$("#ItemType").change(function(){
var ItemType = $(this).val();
$("#Treatment").change(function(){
var Treatment = $(this).val();
最佳答案
更改事件处理程序中的 $(this).val()
将无法获取两个字段的数据,
而是分别获取 ItemType 和 Treatment 的数据
$("#ItemType, #Treatment").on('change', function(){
var ItemType = $("#ItemType").val();
var Treatment = $("#Treatment").val();
$.ajax({
type: "POST",
url: "GetPrice.php",
data: {ItemTypeID: ItemType, TreatmentID: Treatment},
success: function(result){
$("#Price").val(result);
}
});
});
关于php - Ajax 多下拉返回值- php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713262/