Claudio Delgado
Claudio Delgado

Reputation: 2349

Changing the submitted value onSubmit using jQuery

I have two fields that usually work as they should and submit. their IDs and names are 'location_names' and 'sub_cat'

My script is complete except for the fact that I want the 'sub_cat' field value to be ignored (it is completely ignored now) only when the value is '-10' and for it to replace 'location_names' if the value IS NOT '-10'. I had several failed attempts at doing this, all of them just ignore the values or return null. I wonder if I should use regular javascript instead? Any help at all is appreciated.

Code description:

 onSubmit{
 if (sub_cat != '-10') {
 location_names.value = sub_cat.value;
 }
 else {
 // do nothing
 }
 }

Upvotes: 1

Views: 374

Answers (3)

Frias
Frias

Reputation: 11281

I suppose sub_cat is the element, i.e, sub_cat = $('#sub_cat');

In this case:

onSubmit {

    if (parseInt(sub_cat.val(), 10) != -10)
         location_names.val() = sub_cat.val();

    // Add this if you don't want to submit the form when the value is -10.
    return false;

}

Upvotes: 0

Chris Beemster
Chris Beemster

Reputation: 362

You mean something like this?

  • a "return false" statement prevents you from actually submitting the form
  • basically, by creating a $("#form").submit event handler in JavaScript (jQuery), you'll be able to catch the event before actually submitting the data to the server.

Upvotes: 1

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

I guess you should some thing like below when caparisoning of numbers in JS

if (parseInt(sub_cat, 10) != -10) {
   location_names.value = sub_cat.value;
}

Hope this will help you.

Upvotes: 0

Related Questions