vendredi 7 décembre 2018

How to use javascript to get the value of checked checkbox dynamically

I'm new to the javascript and hope someone here can help me with this. I have a webpage that the users can search the document ID and add it to their favourite. after submitting the search criteria, it shows a list ID and a checkbox next to it. so the user can check the checkbox or uncheck it to add and remove them from their list.

My issue is my code can't get the value of the checkbox generated. for example, there are three checkbox generated, chk1,chk2,chk3. when none of them are checked, my code is working I can get the value of the checkbox. but when one of them is checked for example, chk3 is checked, when I check chk1, it still shows the value of chk3 rather than chk1. I want to get the value of that checkbox just checked. I'm struggled to make it right. please could you help.

  <tr><%do until results_rs.EOF%>
   <td class="tdid"><%Response.Write results_rs("id")%></td> 
   <td><input type="checkbox" id="myCheckbox" name ="myf[]" value="<%=results_rs("id")%>" onchange="myfc()">
   <script>

     function myfc(){
     var selchb = getSelectedChbox(this.form);
     alert(selchb)
     }


     function getSelectedChbox(frm) {
         var selchbox = null;
           var chk_arr=document.getElementsByName("myf[]")
           var chklength=chk_arr.length

           for (k = 0; k < chklength; k++) {
               if (chk_arr[k].checked == true)
                 selchbox=chk_arr[k].value
               }
             return selchbox

       **strong text**// rs.close;
       // connection.close
       } 

</script></td>
    <%results_rs.MoveNext%>
    </tr>




Aucun commentaire:

Enregistrer un commentaire