views:

39

answers:

2

I have a form with some input texts passed in GET, and i don't want to have in GET all the fields; i want to avoid empty fields.

So, a concrete example for:

<form method="GET" action="an_url">
    <input type="text" name="field1"/>
    <input type="text" name="field2"/>
    <input type="text" name="field3"/>
    <input type="submit" value="submit"/>
</form>

I supposed that fields disabled by html attribute "disabled" shouldn't be passed to GET.

So i have done a js (based on jquery) to disable empty fields on submit, something like this:

     $("form").submit(function() {
         $(this).find("input[type=text]").each( function () {

            if (!$.trim($(this).val())) {
                $(this).attr("disabled", "true");
            }
         });
     });

However, this doesn't work. Any ideas?

+1  A: 

You can remove the name attribute like this:

 $("form").submit(function() {
     $(this).find("input[type=text]").each( function () {

        if (!$.trim($(this).val())) {
            $(this).removeAttr("name");
        }
     });
 });

From the W3C Recommendation:

When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form. Those controls for which name/value pairs are submitted are called successful controls.

Jacob Relkin
+3  A: 

The disabled attribute accepts one, and only one value: disabled. Setting it to true is not allowed.

(Setting the disabled property is a different story, it is either true or false)

David Dorward
+ for xhtml valide
Andreas Niedermair