1

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
 }
 }

3 Answers 3

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Almost... I managed to get the coding to work and after debugging it with alert() I know what I'm missing... I only need to know how I can replace the values since $mainCats===$subCats; doesn't work. So what's the correct way to do it?
Most helpful reply but still my code didn't work. Thanks anyway
0

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.

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.