Web Analytics Made Easy -
StatCounter Enabling a combo box when a checkbox has been selected - CodingForum

Announcement

Collapse
No announcement yet.

Enabling a combo box when a checkbox has been selected

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enabling a combo box when a checkbox has been selected

    All,

    Some time ago i worked (with help from Dave Clark) on some code to enable 3 combo boxes when 'Yes' was selected from a combo box, i.e - you have a combo with yes or no - if yes was selected the 3 combo's would be enabled (3 combos are for the date). I would like to modify this now so that when a checkbox is checked it will enable the combo's.

    Here is the code that i have editted but cannot get to work - please note that the number of checkbox's are not static.

    <script language="Javascript">
    function checkDisabled(ele, grp) {
    var frm = ele.form;
    if (ele.selectedIndex==1) {
    frm.cboDateDay[grp].disabled=false;
    frm.cboDateMonth[grp].disabled=false;
    frm.cboDateYear[grp].disabled=false;
    } else {
    frm.cboDateDay[grp].disabled=true;
    frm.cboDateMonth[grp].disabled=true;
    frm.cboDateYear[grp].disabled=true;
    }
    return true;
    }
    </script>

    <% if not rsOnArrivalDetails.EOF then
    intCount=0
    Do while not rsOnArrivalDetails.EOF %>


    <tr>
    <INPUT type="hidden" name="UpdateOnArrivalID" value="<%=rsOnArrivalDetails("OnArrivalID")%>">
    <td width="50%" bgcolor="#F5F5F5"><%=rsOnArrivalDetails("Description")%></td>
    <td width="10%" bgcolor="#F5F5F5"><center>
    <input type="checkbox" name="chkNotifyUpdate" disabled value="<%=rsOnArrivalDetails("OnArrivalID")%>" checked></td></center>
    <td width="10%" bgcolor="#F5F5F5"><center>
    <% if rsOnArrivalDetails("Confirmation") = true then %>
    <input type="checkbox" name="chkConfirmUpdate" disabled value="<%=rsOnArrivalDetails("OnArrivalID")%>" checked ></center>
    <% else %>
    <input type="checkbox" name="chkConfirmUpdate" value="<%=rsOnArrivalDetails("OnArrivalID")%>" onChange="return checkDisabled(this, <%=intCount%>)" ;></center>
    <% end if %>
    </td>
    <td width="30%">
    <select name="cboDateDay" disabled>
    <option value="">
    <% For i = 01 to 31 %>
    <option value="<%=i%>"><%=i%></option>
    <% Next %>
    </select>
    </select>
    <select name="cboDateMonth" disabled>
    <option value="">Please Select</option>
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
    </select>
    <select name="cboDateYear" disabled>
    <%CurrentYear = Year(Date)%>
    <option value="">
    <option value="<%=CurrentYear - 1%>"><%=CurrentYear - 1%></option>
    <option value="<%=CurrentYear%>"><%=CurrentYear%></option>
    <option value="<%=CurrentYear + 1%>"><%=CurrentYear + 1%></option>
    </select>
    </font></td>
    </tr>

    <%
    'move through recordset retrieving all rows
    rsOnArrivalDetails.MoveNext
    intCount=intCount+1
    Loop


    If anyone can spot what i am doing wrong then please say!

  • #2
    this is basically a javascript question not an asp one, but anyway, here's the answer:

    function checkDisabled(ele, grp) {
    var frm = ele.form;
    if (ele.checked) {
    frm.cboDateDay[grp].disabled=false;
    frm.cboDateMonth[grp].disabled=false;
    frm.cboDateYear[grp].disabled=false;
    } else {
    frm.cboDateDay[grp].disabled=true;
    frm.cboDateMonth[grp].disabled=true;
    frm.cboDateYear[grp].disabled=true;
    }
    return true;
    }
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      Super, Ta

      Had to include a onClick="this.blur();" in the checkbox properties as it was not changing correctly.

      Thanks

      Comment


      • #4
        Don't suppose you can see why it works great when there are many checkbox's when its looped through the recordset but when there is only one checkbox to display after looping the validation doesn't work - there has to be 2.

        Comment


        • #5
          anyone any ideas to why is doesn't work correctly when there is only 1 in the loop whilst it works great when there is >1?

          Comment


          • #6
            This script is assuming there's more than one on a page - I would first check the length of the variable:

            frm.cboDateDay.length (and/or the others)

            and then send it to a different script if the length is only 1.

            P.S. I'm going to move this to the javascript programming forum, so you can get more help!
            Last edited by whammy; Jul 1, 2002, 06:58 PM.
            Former ASP Forum Moderator - I'm back!

            If you can teach yourself how to learn, you can learn anything. ;)

            Comment


            • #7
              Originally posted by whammy
              This script is assuming there's more than one on a page - I would first check the length of the variable:

              frm.cboDateDay.length (and/or the others)

              and then send it to a different script if the length is only 1.

              P.S. I'm going to move this to the javascript programming forum, so you can get more help!

              This is much easier. Name your select tags like this:

              <select name="cboDateDay<%=intCount%>" disabled>

              <select name="cboDateMonth<%=intCount%>" disabled>

              <select name="cboDateYear<%=intCount%>" disabled>


              Then in the function:

              function checkDisabled(ele, grp) {
              var frm = ele.form;
              if (ele.checked) {
              eval("frm.cboDateDay"+grp +".disabled=false");
              eval("frm.cboDateMonth"+grp +".disabled=false");
              eval("frm.cboDateYear"+grp +".disabled=false");
              } else {
              eval("frm.cboDateDay"+grp +".disabled=true");
              eval("frm.cboDateMonth"+grp +".disabled=true");
              eval("frm.cboDateYear"+grp +".disabled=true");
              }
              return true;
              }
              Glenn
              vBulletin Mods That Rock!

              Comment


              • #8
                Glenn,

                Thats great, just one quick question.

                To get the values out for the dates i previously did:

                for i = 1 to Request.form("chkConfirmUpdate").count
                strDate = request.form("cboDateDay")(i) & " " & MonthName(request.form("cboDateMonth")(i), true) & " " & request.form("cboDateYear")(i)
                Response.Write strDate
                Next

                I now get "An array index is out of range." How can i attach the intCount onto the cboDateDay etc when i want to retrieve the dates?

                Thanks for all your help

                Comment


                • #9
                  Try

                  strDate = request.form("cboDateDay" & i)

                  etc...
                  Former ASP Forum Moderator - I'm back!

                  If you can teach yourself how to learn, you can learn anything. ;)

                  Comment


                  • #10
                    Whammy,

                    wow, i just solved it and came back to post the solution and you beat me!

                    strDate = request.form("cboDateDay"&i) & " " & MonthName(request.form("cboDateMonth"&i), true) & " " & request.form("cboDateYear"&i)

                    It works a treat!

                    Comment


                    • #11
                      Complete one problem and i stumble across another!

                      Ok so if there are 3 dates we have:

                      cboDateDay1
                      cboDateMonth1
                      cboDateYear1

                      cboDateDay2
                      cboDateMonth2
                      cboDateYear2

                      cboDateDay3
                      cboDateMonth3
                      cboDateYear3

                      This page is to update someones details - the 'confirm' checkbox enables the date. Once something has been confirmed the date cannot be changed and cannot be unchecked.

                      The problem lies - if the first 2 descriptions have been confirmed and dates have been input, if i later go in and fill in the 3rd description (so cboDateDay3 etc). I have a problem on the next page because i am doing a for loop for 1 to however many have been checked, in this case it will be 1.

                      Here is my code.

                      for i = 1 to Request.form("chkConfirmUpdate").count
                      strDate = request.form("cboDateDay"&i) & " " & MonthName(request.form("cboDateMonth"&i), true) & " " & request.form("cboDateYear"&i)
                      strSQLQuery = "spUpdateOnArrivalConfirmation '" & Request.Form("cboNames") & "','" & Request.Form("chkConfirmUpdate")(i) & "','" & 1 & "','" & strDate & "'"
                      objADODBConnection.Execute(strSQLQuery)
                      Response.Write strSQLQuery
                      Next

                      So in this case Request.form("chkConfirmUpdate").count = 1 and the cboDateDay etc equal 3.

                      Any one know how i can fix this?

                      Comment


                      • #12
                        Hmm... that confused me for some reason. Can I look at the page or maybe you could email me the code?
                        Former ASP Forum Moderator - I'm back!

                        If you can teach yourself how to learn, you can learn anything. ;)

                        Comment


                        • #13
                          make a hidden field with value intCount.

                          <%
                          ...
                          ...
                          rsOnArrivalDetails.MoveNext
                          intCount=intCount+1
                          Loop
                          %>
                          <input type="hidden" name="total" value="<%=intCount%>">


                          then in the next page:

                          total = Request.Form("total")
                          if total<>"" then total = CInt(total) else total=0

                          for i = 1 to total
                          if request.form("cboDateDay"&i)<>"" then
                          strDate = request.form("cboDateDay"&i) & " " & MonthName(request.form("cboDateMonth"&i), true) & " " & request.form("cboDateYear"&i)
                          strSQLQuery = "spUpdateOnArrivalConfirmation '" & Request.Form("cboNames") & "','" & Request.Form("chkConfirmUpdate")(i) & "','" & 1 & "','" & strDate & "'"
                          objADODBConnection.Execute(strSQLQuery)
                          Response.Write strSQLQuery
                          end if
                          Next
                          Glenn
                          vBulletin Mods That Rock!

                          Comment


                          • #14
                            Glenn, thanks for your response.

                            I still have the problem because 'total' will = 3.

                            If i only select the third confirm checkbox and fill in the cboDateDay etc it will be the third that i am filling in (cboDateDay3 etc).

                            Any other ideas? or have i confused you?!

                            Comment


                            • #15
                              Glenn,

                              I'll try and explian abit better.

                              we have 3 confirm checkboxes and 3 dates. so the dates are cboDateday1.......cboDateDay3 (same for month and year).

                              If i only fill in one of the dates (third one - cboDateDay3 etc), on the next page its trying to get the value out of cboDatDay1 etc.

                              Comment

                              Working...
                              X