简体   繁体   中英

How to put parameter from input with ajax post request?

this is my page:

<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"/>

how to put value of the name input in the request when i press the button?

use var name=$('#name').val(); to get value of name filed

<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>
            var name = $('#name').val();
            $.post("controller",
                    {
                      action: "mainbutton",
                      type: "POST",
                      data: { 'name': name },
                    },
                    function(data,status){
                        alert("Data: " + data + "\nStatus: " + status);
                    });
           });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM