目前,我有一個選擇標簽,每次用戶單擊該特定選擇標簽時,我都想通過 AJAX 呼叫填充該標簽。為此,我正在執行以下操作:
查看類:
<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>");
})
}
});
});
控制器類:
function getonClick(){
$modelList = $this->listings_model->getProperties();
echo(json_encode($modelList));
}
型號分類:
function getProperties(){
$this->db->select("id,name");
$this->db->from("crm_project_properties");
$query = $this->db->get();
return $query->result_array() ;
}
但是這樣做之后,在單擊它之前或什至之后,我都無法在我的選擇標簽中獲取任何資料
uj5u.com熱心網友回復:
ypu 可以試試 .on("click") 嗎?它過去對我有用
$(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>");
})
}
});
});
uj5u.com熱心網友回復:
您應該首先編碼對 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>");
})
}
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/366007.html
下一篇:從2列編碼器獲取結果LIKE
