如何在jQuery中获取POST变量 [英] How to get POST variables in jQuery

查看:83
本文介绍了如何在jQuery中获取POST变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用JQuery获取GET和POST变量?

Possible Duplicate:
how to get GET and POST variables with JQuery?

我有以下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>

我需要对此进行ajax调用,

I need to do an ajax call with this, which I'm triggering on:

$("#submit_financials").live('click', function(){
    ...
});

是否有一种方法可以获取在POST中提交的变量,例如选择了哪个选项(我还需要获取大约10个其他变量).还是我需要使用jQuery选择器来获取每个选择器的值?

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?

推荐答案

$("#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
      }
    });
});

如果您使用的是jQuery> 1.7,最好将live()替换为.on(),并且如果可能的话会更好.这样你就可以写

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

$("#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
      }
    });
});

这里#container指向页面加载时属于DOM的#submit_financials持有者.

Here #container point to holder of #submit_financials that belong to DOM at page load.

这篇关于如何在jQuery中获取POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆