好吧,我有一个从数据库返回值的组合框。这一切都是通过ajax 发生的。用户可以单击名为“添加新行”的按钮,将生成一个新行,该新行也将由组合框提供支持。 但是,我需要将“onchange”事件附加到 ajax 返回的组合框。
我尝试过:-
<script type="text/javascript">
$('.combobox').change(function() {
var va = $('.combobox').val();
alert(va);
});
</script>
但这没有用。我无法收到有关 onchange 事件的任何警报。
我必须尝试什么?
这是我的 ajax :-
try
{
$s = $conn->query("SELECT * FROM testing2");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$selectBegin = "";
$selectBegin =
'
<select class="combobox">
<option value=""></option>
';
$innerSelectString = "";
$selectMain = "";
while($customers = $s->fetch(PDO::FETCH_OBJ))
{
//fix the select thing here
$si = "<option value='$customers->indexid'>$customers->v1</option>";
$selectMain .= $si;
}
$selectEnd = '</select>';
$selectFinal = $selectBegin . $selectMain . $selectEnd;
return $selectFinal;
有多个组合框,我有这个 jquery 代码来运行组合框:-
function load_content_rows()
{
$(document).ready(function() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "ajax.php?requestid=1",
dataType: "html", //expect html to be returned
success: function(response){
$("#requestMaker").html(response);
$("#requestMaker").find(".combobox").combobox();
//alert(response);
}
}); //ajax request end
});
} //load_content
load_content_rows();
最佳答案
从 AJAX 选项中删除这部分,这可能会避免很多问题:
dataType: "html", //expect html to be returned
因此您的 AJAX 请求将如下所示:
$(document).ready(function() {
function load_content_rows() {
$.ajax({ //create an ajax request to load_page.php
type: "GET",
url: "ajax.php?requestid=1",
success: function(response){
$("#requestMaker").html(response);
$("#requestMaker").find(".combobox").combobox();
// alert(response);
}
}); // ajax request end
} //load_content
load_content_rows();
});
关于javascript - 对ajax返回的选择值添加jquery onchange效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32780639/