开发者

Getting GET request string in jQuery

开发者 https://www.devze.com 2023-02-13 03:37 出处:网络
suppose I have a form: <form id=\"someform\" action=\"some.php\" method=\"get\"> <input type=\"text\" name=\"somename\" />

suppose I have a form:

<form id="someform" action="some.php" method="get">
    <input type="text" name="somename" />
    <input type="text" name="oth开发者_开发百科ername" />
    ...
    <input id="submitId" type="submit" value="OK" />
</form>

And I want to get the request string generated by this:

?somename=blabla&othername=blablabla&submit=OK

Can I get this without actually submitting the form?

$('#submitId').live('click', function(e) {
    e.preventDefault();
    //... what to do here?
});

Another one, if I have the request string, can I put them to the form? (reversed).

Thanks.


Try using http://api.jquery.com/serialize/

$('#submitId').live('click', function(e) {
    e.preventDefault();
$('#someform').serialize();
});


try jQuery serialize:

$('#someform').serialize();


I think you want to submit form using jQuery. You can use jQuery.ajax like this:

jQuery('#someform').live('submit',function(e) {
    $.ajax({
        url: $(this).attr('action'), // get action from form tag
        type: 'GET', // GET method
        data: $(this).serialize(), // Get form values
        success: function( response ) {
            alert( response ); // response from action script
        }
    });
    e.preventDefault();
});

Now in some.php action you can get form values like this:

echo $_GET['somename'];
echo $_GET['othername'];
0

精彩评论

暂无评论...
验证码 换一张
取 消