javascript 如何在 jQuery 中获取 POST 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10886039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get POST variables in jQuery
提问by David542
Possible Duplicate:
how to get GET and POST variables with JQuery?
I have the following HTML:
我有以下 HTML:
<form action='.' method='post'>{% csrf_token %}
<div class="parameters">
Show
<select name="earnings_filter">
<option value="all">Total earnings</option>
<option value="hd">HD earnings</option>
<option value="sd">SD earnings</option>
</select>
<input type="submit" name="submit" class="submit float-right" value="submit" id="submit_financials"/>
</div>
</form>
I need to do an ajax call with this, which I'm triggering on:
我需要用这个做一个ajax调用,我正在触发:
$("#submit_financials").live('click', function(){
...
});
Is there a way to get the variables that are submitted in POST, for example which option was selected (and there are about 10 other variables I need to get). Or do I need to use the jQuery selectors to get the value of each?
有没有办法获取在 POST 中提交的变量,例如选择了哪个选项(我需要获取大约 10 个其他变量)。或者我是否需要使用 jQuery 选择器来获取每个的值?
回答by thecodeparadox
$("#submit_financials").live('click', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});
It would be better replace live()
with .on()
if you're using jQuery > 1.7 and it'd be better if possible. So you can write it
如果您使用的是 jQuery > 1.7,最好替换为live()
,.on()
如果可能的话会更好。所以你可以写它
$("#container").on('click', '#submit_financials', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});
Here #container
point to holder of #submit_financials
that belong to DOM at page load.
这里#container
指向#submit_financials
页面加载时属于 DOM 的持有者。
回答by Todd Gibson
If all the values are in input elements on the form...
如果所有值都在表单上的输入元素中...
$("#formId").serialize()
回答by Shyju
You could serialize
the form and send to the server page
您可以serialize
将表单发送到服务器页面
$.post("yourServerPage.php", $("form").serialize(),function(data){
//Do whatever with the result from ajax server page.
});
回答by Дамян Станчев
How about creating several input values of type hidden
如何创建多个隐藏类型的输入值
<input type="hidden" id="sample_id" value="SOMETHING" />
give them ids and acces the data inside using:
给他们 ids 并使用以下方法访问数据:
$('#sample_id').val()
回答by Andrew Odri
Unfortunately, POST variables are communicated in the request from the client to the server, but they are not automatically included in the response from the server back to the client. This is one of the things that sets them apart from GET variables.
不幸的是,POST 变量在从客户端到服务器的请求中传递,但它们不会自动包含在从服务器返回到客户端的响应中。这是将它们与 GET 变量区分开来的原因之一。
If your data isn't excessively long, you may want to switch to the GET method instead. You can then retrieve the variables using a function like one listed in this question: How can I get query string values in JavaScript?
如果您的数据不是太长,您可能希望改用 GET 方法。然后,您可以使用如下问题中列出的函数检索变量:如何在 JavaScript 中获取查询字符串值?
Alternatively, if you have access to the server-side code, you could include these variables in HTML returned; however this is another question entirely :)
或者,如果您有权访问服务器端代码,则可以在返回的 HTML 中包含这些变量;然而,这完全是另一个问题:)