Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore input fields on form submit with jQuery

I would like to include only certain form fields when submitting a form with jQuery. For instance, I would like to ignore all fields that have the disabled attribute... maybe something like this:

$('#my_button').click(function() {
    if($('input').hasAttr('disabled') {
        //ignore this form field when submitting
    }
    $('form').submit();
});

Any ideas?

like image 981
PropSoft Avatar asked Nov 07 '10 19:11

PropSoft


People also ask

How do you prevent form submit on Enter in jQuery?

$(document). on('keyup keypress', 'form input[type="text"]', function(e) { if(e. keyCode == 13) { e. preventDefault(); return false; } });

Do not submit form if field is empty jQuery?

One of the most used logic related to submitting a form is preventing sumbission if a field is empty. In this example we'll provide examples with source code how to prevent form submit in case of empty field. The main trick here is using . preventDefault() function to stop the submission.

How do you prevent a form from clearing fields on submit?

You can use e. preventDefault() or return false; within a jQuery event handler: $("#Formid").

How do you prevent form submit on Enter in JavaScript?

Use preventDefault() event method to Prevent form submission on the “Enter” key pressed in JavaScript. Enter key keycode is 13, so you can check in if statement.


1 Answers

That's not necessary, disabled inputs are excluded already. The various jQuery methods, including submit() and serialize() honour the HTML 4 specification for disabled controls in web forms, which goes as follows:

A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

However:

  • Controls that are disabled cannot be successful.

http://www.w3.org/TR/html401/interact/forms.html#successful-controls

like image 168
Andy E Avatar answered Sep 24 '22 21:09

Andy E