将下面的js代码复制到你有下拉菜单的主文件中:-
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('select').change(function(){
var dwxvar = $(this).val();
$.ajax({
type: "POST",
url: "http://localhost/dwx/phpcodefile.php",
data: {dwx:dwxvar},
cache: false,
dataType: 'json',
success: function(data_val){
alert(data_val);
$("#result").html(data_val);
}
});
});
});
</script>
我假设这是您的下拉菜单:-
<select>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<option value="4">Value 4</option>
</select>
您必须使用输出所需的唯一代码(php)制作一个单独的 php 文件(例如 phpcodefile.php):-
<?php
$varname = $_POST["dwx"]; // this is the same variable which we have sent here data: {dwx:dwxvar},
// -- now using this value perform whatever action you want to perform below --
//Next try to put the result in a variable so as to return value to ajax call as we do for functions with return type. Suppose the variable name is **$alldata**
echo $alldata;
?>
您在 phpcodefile.php 文件上打印的内容将被返回,您将能够在 alert() 中看到它。这意味着所有数据现在都在“data_val”这里
success: function(data_val){
alert(data_val);
}
现在您必须将此 data_val 附加到某个标签
<div id="result"></div>
喜欢这个
$("#result").html(data_val);
尝试以这种方式执行您的代码,它会按照您想要的方式运行。
如果您发现任何问题,那么我需要查看您的完整 html 和 php 代码。