如何使用ajax post请求输入参数?

时间:2015-06-12 08:01:03

标签: javascript jquery html ajax

这是我的页面:

<script type="text/javascript">
$(document).ready(function() {
    $("#mainbutton").click(function() {
        $.post("controller", {
            action: "mainbutton",
            name: "name"
        },
        function(data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>


<input id = "name" type="text" placeholder="Name" name="" /><span></span>
<input id="mainbutton" class="mainbutton" type="button" value="Rush my trial"/>

按下按钮时如何在请求中输入名称输入的值?

2 个答案:

答案 0 :(得分:2)

使用var name=$('#name').val();获取名称提交的值

<script type="text/javascript">
        $(document).ready(function(){
            $("#mainbutton").click(function(){
                var name=$('#name').val();
                $.post("controller",
                        {
                          action: "mainbutton",
                          name: name// pass parameter like this

                        },
                        function(data,status){
                            alert("Data: " + data + "\nStatus: " + status);
                        });
            });
        });
        </script>

答案 1 :(得分:0)

            var name = $('#name').val();
            $.post("controller",
                    {
                      action: "mainbutton",
                      type: "POST",
                      data: { 'name': name },
                    },
                    function(data,status){
                        alert("Data: " + data + "\nStatus: " + status);
                    });
           });