Jump to content

Hi, how to have jquery check if one of several select menus have quantity select allow submit...


mac007
Go to solution Solved by mac_gyver,

Recommended Posts

Hi all, I have this JQUERY/JAVASCRIPT SNIPPET right now that will check if an item has a quantity selected out of a long list  of product select-menus - it works fine, EXCEPT, I want it to work so it allows it to submit even if the item they may have clicked was NOT the one that had the quantity selected. In other words, not have the submit button tied to the specific item its on, but to the fact that there has been an item somewhere in the form that has a quantity selected. Right now, the way it is, person has to click any of the items that have a quantity selected. The problem is if a person goes all the way down a long list of products, I want them to simply click on any submit button from whereever they are, and not have to climb back up the page to click on the submit button of any of the products they did chose a quantity for...

 

This is the code here:

 

 

 
$(document).ready( function()
{
    $('input[type="image"]').click( function()
    {
        var quantity = new Array;
        quantity[1] = $(this).closest('tr').find('select').val();

        if(quantity[1] == 0)
        {
            alert("You have not selected a product quantity!");
            return false;
        }
    });
});
 

 

 

THIS IS THE BASIC HTML FOR ONE OF THE PRODUCTS... ITS A LONG LIST OF LIKE 50 PRODUCTS, ONE AFTER THE OTHER...

 

    

 

PRODUCT A  (SAY I SELECT  QUANTITY ONE HERE ON THIS PRODUCT)   <-----------------
        <td width="10%" align="center" valign="middle" >Qty:
            <select   class="qty" name="qty189" id="qty_189" >
              <option value="0">0</option>
              <option value="1">1</option>
            </select>
            <br />
            <input type="hidden" name="action" value="AddCart" />
            <input type="hidden" name="process_type" value="2" />
        <br />
        <input name="submit" type="image" value="Add To Cart" src="offer-button.gif" />
        </td>

 

NEXT PRODUCT B

 

NEXT PRODUCT C

 

NEXT PRODUCT D

 

NEXT PRODUCT E   (BUT PERSON CLICKS ON THIS SUBMIT BUTTON - ALLOW IT SINCE THERE'S  A QUANTITY SELECTED FOR PRODUCT A) <-----------------

 

ETC, ETC...

 

Appreciate any help...

Thanks!

Link to comment
Share on other sites

  • Solution

assuming that you want to look at all the select/option menus and they all have class='qty', you would loop over each of the elements with that class - 

$(document).ready( function()
{
    $('input[type="image"]').click( function()
    {
        var total = 0;
        $( ".qty" ).each(function() {
            total += Number($( this ).val());
        });

        if(total == 0)
        {
            alert("You have not selected a product quantity!");
            return false;
        }
    });
});
Link to comment
Share on other sites

Wow, you are AWESOME Mac_giver!  It worked like a charm!  :) Thanks a lot for your help... I'm more of web-designer who dabbles here and there with PHP and javascript when needed... so my knowledge on it is very limited as much of my time is spent so much on design aspect. Again, thanks a lot!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.