我有以下内容,但由于未显示警报,因此postdata似乎存在问题。如何正确创建后数据以包含所选选项?
HTML
<select id="reportTypeChosen" onchange="reportType()">
<option value="1">Opt1</option>
<option value="2">Opt2</option>
<option value="3">Opt3</option>
</select>
脚本
function reportType() {
var sel = document.getElementById("reportTypeChosen");
var choosenType = sel.options[sel.selectedIndex].value;
var postdata = "{choice: " + choosenType + "}";
$.post("setreporttype.php", function(postdata, status){
alert("Data: " + data + "\nStatus: " + status);
});
}
这会在控制台中显示错误:未定义数据
我也尝试过:
function reportType() {
var sel = document.getElementById("reportTypeChosen");
var choosenType = sel.options[sel.selectedIndex].value;
$.post("setreporttype.php", function({choice: choosenType}, status){
alert("Data: " + data + "\nStatus: " + status);
}
这也不起作用,但没有任何错误。
更新: postdata的位置不正确。更正了以下版本:
function reportType() {
var sel = document.getElementById("reportTypeChosen");
var choosenType = sel.options[sel.selectedIndex].value;
$.post("setreporttype.php", {choice: choosenType}, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
}
答案 0 :(得分:0)
您正在将post函数与两个参数一起使用,即URl和成功回调,因此您要发送一个空的帖子(尽管调试时请更清楚),然后查找要发送的数据在回调的回复中。
您可能想要这样:
$.post({my-url}, {data-to-send}, function({data-from-server}){
doStuff(data-from-server)
});