Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a submit button and gather form data

Tags:

html

jquery

forms

I have a web page like this:

<form id="some-form">
   <input type='text'>
   <button type="submit">
</form>
. . . later in the page . . .
<div id="more-inputs">
   <input type="checkbox">
   <input type="text">
</div>

For various reasons, the later inputs and check boxes can't go in the original form, but when the form is submitted, I want to use jQuery to gather the values from the checkboxes and the other forms, and submit them as a GET request.

like image 625
mlissner Avatar asked Dec 20 '11 21:12

mlissner


People also ask

Can I have submit button outside of form?

You can tie a submit button to a form that the button doesn't live inside of. The trick is to give the form an id and then reference that id with the button's form property. With this setup, clicking the Submit button will cause the form to be submitted.

How do I disable the submit button?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do I capture a form submit response?

That is, the submit() function doesn't actually return anything, it just sends the form data to the server. If you really wanted to get the response in Javascript (without the page refreshing), then you'll need to use AJAX, and when you start talking about using AJAX, you'll need to use a library.


1 Answers

Just trap the submit event for your form?

$('#some-form').submit(function(){

    // do work
    // do a jquery get or whatever you need to do...
    $.get('myurl.html', function(data) {

    });

    return false; // return false to prevent typical submit behavior

});
like image 59
Gabe Avatar answered Oct 26 '22 22:10

Gabe