我一直在尝试使用 php 和 ajax 来更改查询。事情是这样的
我有一个下拉菜单,更改后它会将所选数据复制到我使用 javascript 实现的文本框,如下所示。
$(document).ready(function () { $("#animaloption").change(function () { $('#animalfilter').val($("#animaloption").val()); }); });
然后
我需要检查 id 为 animalfilter 的输入文本框是否已更改,然后使用 ajax 查询它,我不知道这个是否正确,因为我无法检索返回
$(document).ready(function () { $("#animaloption").change(function () { $('#animalfilter').val($("#animaloption").val()); var animal= $('#animalfilter').val($("#animaloption").val()); $.ajax({ url: "ajax/test.php", type: "post", data: animal, success: function (response) { $("#kind").html(response); } }); }); });
带有id = kind的输入不会显示任何内容。
在我的 test.php 中
<?php
$a = "SUCCESS";
return $a;
?>
请大家帮忙。
谢谢。
最佳答案
您在这里遇到了几个问题,我将记下您关注的领域:
jQuery:
$(document).ready(function() {
// Not necessary, but better to use .on() here
$("#animaloption").on('change',function() {
// Use $(this) instead, grabs the selected value. No sense in
// capturing the same event value when you have it in this event already
var animal = $(this).val();
// Assign value here to field
$('#animalfilter').val(animal);
// Start ajax
$.ajax({
// Make sure this is the correct path
url: "ajax/test.php",
type: "post",
data: animal,
success: function(response) {
$("#kind").html(response);
}
});
});
});
/ajax/test.php
<?php
$a = "SUCCESS";
# You have to echo, not return
echo $a;
# Try print_r() for sure-fire results testing
print_r($_POST);
如果这些更改不能解决问题,则需要查看您的表单 html。
关于javascript - 使用ajax和php根据文本框更改更改查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856628/