help needed with form selectedIndex property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • % =joe %

    #1

    help needed with form selectedIndex property

    I cannot get this code to work. Very simple...I have three list
    menus. I want to do a check before the form submits to make sure that
    the value of the 3 fields is equal to 12.


    Here's my code...

    <script language="javas cript">
    <!--
    function addListValues() {
    var grandtotal = 0;
    var drop1Value,drop 2Value,drop3Val ue;

    var drop1Value=docu ment.cakeForm.i ntQuant1.option s[document.cakeFo rm.intQuant1.op tions.selectedI ndex].value;
    var drop2Value=docu ment.cakeForm.i ntQuant2.option s[document.cakeFo rm.intQuant2.op tions.selectedI ndex].value;
    var drop3Value=docu ment.cakeForm.i ntQuant3.option s[document.cakeFo rm.intQuant3.op tions.selectedI ndex].value;

    grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);

    if (grandtotal < 12) {
    alert( "You do not have enough items selected. You need to choose
    12 cheesecakes." );
    cakeForm.intQua nt1.focus();
    document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
    return false ;
    }

    if (grandtotal > 12) {
    alert( "You have too many items selected. You need to choose 12
    cheesecakes." );
    cakeForm.intQua nt1.focus();
    document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
    return false ;
    }

    if (grandtotal = 12) {
    document.cakeFo rm.submit();
    //return false ;
    }
    }
    //-->
    </script>


    Here is one of my three list menus:

    <select name="intQuant1 ">
    <option value="0">0</option>
    <option value="12">12</option>
    <option value="4">6</option>
    <option value="4">4</option>
    </select>

    My submit buttonhas this call... onClick="return addListValues() ;

    It does not appear to be working for all three list menus.

    Many Thanks
  • Lee

    #2
    Re: help needed with form selectedIndex property

    % =joe % said:[color=blue]
    >
    >I cannot get this code to work. Very simple...I have three list
    >menus. I want to do a check before the form submits to make sure that
    >the value of the 3 fields is equal to 12.
    >
    >
    >Here's my code...
    >
    ><script language="javas cript">[/color]

    The <script> tag should have a type attribute:
    <script type="text/javascript">
    [color=blue]
    ><!--[/color]

    That line does nothing. Remove it and the matching //-->
    [color=blue]
    >function addListValues() {[/color]

    You could simplify the code, and make it more portable, by passing
    arguments into it, instead of making the function know where to
    find the values.

    [color=blue]
    > var grandtotal = 0;
    > var drop1Value,drop 2Value,drop3Val ue;[/color]

    Since you redefine each of these just below, these lines
    don't really add anything (if you add a "var" to your
    assignment to grandtotal, below).
    [color=blue]
    >var
    >drop1Value=doc ument.cakeForm. intQuant1.optio ns[document.cakeFo rm.intQuant1.op tions.selectedI ndex].value;
    >var
    >drop2Value=doc ument.cakeForm. intQuant2.optio ns[document.cakeFo rm.intQuant2.op tions.selectedI ndex].value;
    >var
    >drop3Value=doc ument.cakeForm. intQuant3.optio ns[document.cakeFo rm.intQuant3.op tions.selectedI ndex].value;
    >
    > grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);
    >
    > if (grandtotal < 12) {
    > alert( "You do not have enough items selected. You need to choose
    >12 cheesecakes." );
    > cakeForm.intQua nt1.focus();[/color]

    That should be document.cakeFo rm...[color=blue]
    > document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
    > return false ;
    > }
    >
    > if (grandtotal > 12) {
    > alert( "You have too many items selected. You need to choose 12
    >cheesecakes. " );
    > cakeForm.intQua nt1.focus();[/color]

    Again, document.cakeFo rm...
    [color=blue]
    > document.cakeFo rm.intQuant1.st yle.background = "#DEAA0C";
    > return false ;
    > }
    >
    > if (grandtotal = 12) {[/color]

    The equality comparison operator is "==", not "=".
    [color=blue]
    > document.cakeFo rm.submit();[/color]

    It's better to avoid submit() via Javascript. If you validate
    in the onSubmit handler, then returning false will prevent the
    form from submitting, and returning anything else will allow
    it to submit.
    [color=blue]
    > //return false ;
    > }
    >}
    >//-->[/color]

    Again, these HTML comments are not needed.
    There are no longer any browsers in use that need them.
    If there were, they wouldn't be able to use your form, anyway.
    [color=blue]
    ></script>
    >
    >
    >Here is one of my three list menus:
    >
    ><select name="intQuant1 ">
    ><option value="0">0</option>
    ><option value="12">12</option>
    ><option value="4">6</option>[/color]

    The value should be "6" in that line.

    [color=blue]
    ><option value="4">4</option>
    ></select>
    >
    >My submit buttonhas this call... onClick="return addListValues() ;[/color]

    Should be <form .... onsubmit="retur n addListValues(. ..)">

    Here's a working example. Don't turn it in as your own homework:

    <html>
    <head>
    <title>Say Cheesecake!</title>
    <script type="text/javascript">
    function addListValues(q 1,q2,q3) {
    var total = +q1.options[q1.selectedInde x].value
    + +q2.options[q2.selectedInde x].value
    + +q3.options[q3.selectedInde x].value;
    if(total==12) {
    return true;
    } else {
    alert("You have chosen " + total + " cheesecakes.\n"
    +"You must choose exactly 12.");
    return false;
    }
    }
    </script>
    </head>
    <body>
    <!-- The following line may wrap when posted -->
    <form onsubmit="retur n
    addListValues(t his.intQuant1,t his.intQuant2,t his.intQuant3)" >
    <select name="intQuant1 ">
    <option value="0">0</option>
    <option value="12">12</option>
    <option value="6">6</option>
    <option value="4">4</option>
    </select>
    <select name="intQuant2 ">
    <option value="0">0</option>
    <option value="12">12</option>
    <option value="6">6</option>
    <option value="4">4</option>
    </select>
    <select name="intQuant3 ">
    <option value="0">0</option>
    <option value="12">12</option>
    <option value="6">6</option>
    <option value="4">4</option>
    </select>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>

    Comment

    • Joseph Weiss

      #3
      Re: help needed with form selectedIndex property



      Thanks. I appreciate the insight.

      If you could point me in one direction to 'sharpen my javascript'
      skills, where woul dit be?

      Thanks again

      joe

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Lee

        #4
        Re: help needed with form selectedIndex property

        Joseph Weiss said:[color=blue]
        >
        >Thanks. I appreciate the insight.
        >
        >If you could point me in one direction to 'sharpen my javascript'
        >skills, where woul dit be?[/color]

        This newsgroup.

        Read questions, try to solve the problems by looking
        things up on the web or in this newsgroups FAQ or the
        resources it points to:



        or just by trying things that seem reasonable.
        If you can solve the problem, post your answer.
        If it's wrong or can be significantly improved,
        somebody will let you know.

        Either way, read other people's answers.

        It really works.

        Comment

        Working...