Results 1 to 2 of 2

Thread: Checking that at least one item is selected in loop generated list JavaScript

  1. #1
    Join Date
    Jun 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Checking that at least one item is selected in loop generated list JavaScript

    I have a list generated from a Query with name, quantity and price creating a comma deliminated list that contains a hyphen deliminated list. works great.

    The issue is that I need to confirm that at least one item in any of the rows has been selected before visitor can post page.

    This is the select statement that generates the list:

    <select name="quanty" style="width: 45px">
    <cfloop from="0" to="#qnum#" index="quant">
    <cfif quant LT 10>
    <option value="#ssID#-#quant#-#gtserv2.price#">&nbsp;&nbsp;#quant</option>
    <cfelse>
    <option value="#ssID#-#quant#-#gtserv2.price#">#quant#</option>
    </cfif>
    </cfloop>
    </select>

    This is what the variable would look like if quantity of 1 was selected for the first item... 1801-1-1200.00,1802-0-250.00,1805-0-150.00,1806-0-100.00,1820-0-50.00

    I am looping through a query where each result constitutes it's own form input and it's name is the service id. Sometimes the list can be several items and sometimes 20 or more. Since "0" is the default of each inClick image for larger version. 

Name:	1.jpg 
Views:	338 
Size:	65.1 KB 
ID:	5659put I need to know if one or more fields are larger than 0 and if none are so to trigger an JavaScript message saying "You must select at least one item".. Remember most inputs in the loop will remain as 0.

    Right now I just check on the form to page and tell them to go back and select something. Seems like this would generate so much JavaScript that the page would drag but I wonder if there is a simple solution.

    Tnx
    andy

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    1. Set a variable to 0
    2. Split the string into an array using str.split(",");
    3. Loop through the array and split each member using str.split("-");
    4. Add the value of member[1] in each sub-array to the variable in line 1
    5. On exit from the loop, the variable set in line 1 contains the total number of items selected

Similar Threads

  1. Replies: 0
    Last Post: 01-28-2011, 07:53 PM
  2. Checking list
    By jfreak53 in forum PHP
    Replies: 1
    Last Post: 07-30-2009, 05:51 PM
  3. Replies: 1
    Last Post: 03-20-2009, 10:47 AM
  4. Replies: 5
    Last Post: 05-19-2008, 04:44 AM
  5. Hide list item when another is selected
    By billyboy in forum JavaScript
    Replies: 2
    Last Post: 11-28-2006, 06:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •