![](/img/trans.png)
[英]How to get <p> tag value from XMLHttpRequest response and set in html
[英]How to get value from select tag after Ajax response as html format?
目的:我在Jquery中使用Ajax創建一個下拉列表,從數據庫中選擇數據並發送到html格式的html。
問題:在Ajax響應之后,我無法在我的下拉列表中使用任何選擇器。
描述:我用PHP從數據庫中選擇城市和與選定的標簽回聲 ( <selection></selectio>
以使一個下拉並使用#location從Ajax的數據發送到div標簽的表示browser.My下降到用戶工作得很好但是我不能使用#testing選擇器在用戶更改每個下拉列表的任何值后發出警報 。
PHP功能
public function select_cat($table = FALSE, $where = FALSE) {
$this->db->select('*');
$this->db->from($table);
if ($where) {
$this->db->where($where);
}
$q = $this->db->get();
if ($q->num_rows() > 0) {
echo '<select id="testing" name="p_locat" class="form-control" id="p_locat">';
foreach ($q->result() as $row) {
echo "<option value=" . $row->l_id . ">" . $row->city . "</option>";
}
echo '</select>';
} else {
return FALSE;
}
}
這是My Ajax
$(document).ready(function(){
$.ajax({
url: "<?php echo base_url('ads/get_locat'); ?>",
data: {'<?PHP echo $this->security->get_csrf_token_name(); ?>': '<?PHP echo $this->security->get_csrf_hash(); ?>'},
dataType: "html",
cache: false,
success: function (data) {
console.log(data);
$("#location").html(data);
}
});
})
用戶點擊每個下拉列表后調用Alert() (文檔中的所有JS)
$("#testing").on('change',function(){
alert($("#p_locat"));
})
這是div標簽
<div id="location"></div>
對於動態添加到DOM的元素,您需要$(document)
,如下所示:
$(document).on('change',"select#testing",function(){
alert($('#testing option:selected').val());
//Write stuffs
});
$.ajax({
url: "<?php echo base_url('ads/get_locat'); ?>",
data: {
'<?PHP echo $this->security->get_csrf_token_name(); ?>': '<?PHP echo $this->security->get_csrf_hash(); ?>'
},
dataType: "html",
cache: false,
success: function (data) {
console.log(data);
$("#location").html(data); // Throw HTML in DOM
alert($('#testing option:selected').val()); // Get the selected value
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.