使用 AJAX 请求未在 select 标记中填充数据
Data not populating in select tag with AJAX request
目前我有一个 select 标签,我想在每次用户点击那个特定的 select 标签时通过 AJAX 调用来填充它。为此,我正在执行以下操作:
查看class:
<label class="control-labels ">Property</label>
<select name="property" class="form-control select2 selectsearch" <?php echo (isset($read) ? $read:''); ?> required>
</select>
Ajax 请求:
$(document).ready(function(){
$("#property").click(function(){
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
$.each(response, function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});
控制器Class:
function getonClick(){
$modelList = $this->listings_model->getProperties();
echo(json_encode($modelList));
}
型号Class:
function getProperties(){
$this->db->select("id,name");
$this->db->from("crm_project_properties");
$query = $this->db->get();
return $query->result_array() ;
}
但是在这样做之后,我无法在我的 select 标签中获取任何数据,在点击它之前甚至之后
您应该先对您对 JSON 对象的响应进行编码
$(document).ready(function(){
$("#property").click(function(){
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
var responseText = JSON.parse(response);
$.each(responseText , function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});
你能试试 .onclick") 吗?
它过去对我有用
$(document).ready(function () {
$("#property").on("click", '*:not(a)', function() {
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
$.each(response, function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});
目前我有一个 select 标签,我想在每次用户点击那个特定的 select 标签时通过 AJAX 调用来填充它。为此,我正在执行以下操作:
查看class:
<label class="control-labels ">Property</label>
<select name="property" class="form-control select2 selectsearch" <?php echo (isset($read) ? $read:''); ?> required>
</select>
Ajax 请求:
$(document).ready(function(){
$("#property").click(function(){
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
$.each(response, function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});
控制器Class:
function getonClick(){
$modelList = $this->listings_model->getProperties();
echo(json_encode($modelList));
}
型号Class:
function getProperties(){
$this->db->select("id,name");
$this->db->from("crm_project_properties");
$query = $this->db->get();
return $query->result_array() ;
}
但是在这样做之后,我无法在我的 select 标签中获取任何数据,在点击它之前甚至之后
您应该先对您对 JSON 对象的响应进行编码
$(document).ready(function(){
$("#property").click(function(){
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
var responseText = JSON.parse(response);
$.each(responseText , function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});
你能试试 .onclick") 吗? 它过去对我有用
$(document).ready(function () {
$("#property").on("click", '*:not(a)', function() {
var post_url = 'listings/getonClick'
$.ajax({
type: "POST",
url: post_url,
data : { "hid" : $(this).val() },
success: function(response){
$.each(response, function(value, key) {
$("#property").append("<option id="+value.id+">"+value.name+"</option>");
})
}
});
});