views:

67

answers:

2

hi, i have form that have about 5 fields which final query created by processing values that fields. i want to send only final query not all fields to server. can i exclude some fields from submitting (with jquery)?

<form action="abc/def.aspx" method="get">
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    <input type="text" name="field3" />
    <input type="text" name="field4" />
    <input type="text" name="field5" />
    <input type="hidden" name="final" />
    <input type="submit" value="Send" />
</form>

which generate url like this:

abc/def.aspx?field1=val1&field2=val2&field3=val3&field4=val4&field5=val5&final=finalQuery
+5  A: 

Remove the element on submit.

On the onsubmit handler:

$(formElement).submit(function() {
    $(this.field1).remove(); //removing field 1 from query
    return true; //send
});

Disabling the form element also stops it from being entered into the query.(tested on Chrome)

$(formElement).submit(function() {
    this.field1.disabled = true;
    return true; //send
});
digitalFresh
+1 - For a full version, just `$(':text', this).remove();` works, no need for a return either.
Nick Craver
Alternatively, the script could just disable the inputs. I guess it doesn't really matter if it's a plain form submit, but if it were an Ajax form submission you might want the fields to be there still afterwards, and it's a little easier to just set "disabled" to false than to put the inputs back.
Pointy
hm... using your approach disabled form fields even if form validation is not valid. how i can check if form validation is true then disable fields?
Sadegh
if form validation is in a function make it return a boolean and ask for that before doing the digitalFresh solution, for a more flexible solution maybe you should use something like jquery ajaxForm http://jquery.malsup.com/form/ it has options for validation/presubmit methods, callbacks and all kind of useful things
SinneR
+1  A: 

I think the best solution is to handle the submit and then send the request yourself:

$(form).submit(function() {
    //do everything you need in here to get the final result
    var finalResult = alotoflogic();
    $.get("abc/def.aspx",final=finalResult, "callbackfunction", "responseType");
    return false;
});

that should do exactly what you want. EDIT: as Alex pointed out this solution wouldnt send you to that page, just get the results if you need to go to the new page you can do:

$(form).submit(function() {
    //do everything you need in here to get the final result
    var finalResult = alotoflogic();
    window.location('abc/def.aspx?final='+finalResult);
    return false;
});

This way the browser is redirected to that page and only the final result is send.

SinneR
A normal form submission will redirect the browser to the new location. As is, this would not do that - it would just fetch the results of the GET and then... who knows, depending on the call back function.
Alex JL
good point alex, just edited to add the non ajax solution ;) ty
SinneR