我的站点使用的是jQuery,我在项目中有一个更新部分,还有三个下拉列表:
我试图使用jQuery创建一个Ajax调用,当选择它时,可以调用$.ajax()并将数据发送到我的php页面以更新MySQL数据库。我猜我能做到:
获取SELECT中的数据并将其存储在变量中,并使用$.ajax将其作为数据发送,但我不确定在选择并运行Ajax之后如何捕获值?
有什么建议吗?
I would use the jQuery post method.. something like:
$.post("updater.php", { completed: variableWithCompletedFromSelect } );
Just call that in the on change event on the select, and pass in the select's value. Then your php script will have $_POST['completed'] with the new value.
As FryGuy said the $.post method is probably your best bet although I'd lay it out like this:
$("#project select").change(function(){ // use one selector for all 3 selects
$.post("someurl.php",{
// data to send
completed: $("select#completed").val(),
hours: $("select#hours").val(),
who: $("select#who").val()
}, function(data) {
// do callback stuff with the server response 'data' here
});
});
This might not be exactly what you wanted if the three selects occur more than once on the page. I'd recommend reading through the jquery docs to get a handle on events. As for pulling out data you just need to use selectors and methods like .text()
, .val()
, .html()
, .attr()
etc... at the point you need them. The information you want is always in the DOM somewhere.