虽然我发送了值并使用方法POST但是当我调用方法POST时它没有值
$("select#fieldCity").change(function() {
var id = $("select#fieldCity option:selected").attr("value") ?
$("select#fieldCity option:selected").attr("value") : 0;
console.log(id);
$.ajax({
url: '../<?php echo $role; ?>/create',
type: "POST",
data: {
city_id: id
},
});
});
我调用方法POST获取值
$city_id = $_POST['city_id'] ? $_POST['city_id'] : 0;
答案 0 :(得分:0)
您只需使用$(this).val()
而不是attr('value')
即可获得所选值。以下是更新的JS代码:
$("select#fieldCity").change(function() {
var selectedCity = $(this).val();
$.ajax({
url: '../<?php echo $role; ?>/create',
type: "POST",
data: {
city_id: selectedCity
},
});
});
在你的PHP中,你可以:
$city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;