2017-12-11 5 views
0

In meinem Projekt möchte ich Checkbox Wert in Array zu instorieren.Wie bekomme ich Checkbox-Wert und speichere in einem Array

Hier ist mein Code:

$('#fpdId').click(function(){ 
    var files = new Array(); 

    //xzyId is table id. 
    $('#xzyId tbody tr input:checkbox').each(function() { 
     if (this.checked) { 
     files.push(this.value); 
     } 
    }); 

    console.log(files); 
}); 

    <input type="button" id="fpdId" value="filePack"> 

Meine HTML-Tabelle hat drei Zeilen und jede Zeile hat drei tds

Hier Codetabelle:

<table border=1px class="xzy" id="xzyId" style="width:100%"> 
     <colgroup> 
      <col style="width:10%"> 
      <col style="width:80%"> 
      <col style="width:10%"> 
     </colgroup> 
     <tbody> 
     <tr> 
      <td ><input type="checkbox" value="x" >1</td> 
      <td >Stack</td> 
      <td >John</td> 
     </tr> 
     <tr> 
      <td ><input type="checkbox" value="y" >2</td> 
      <td >Stack</td> 
      <td >Sansa</td> 
     </tr> 
     <tr> 
      <td ><input type="checkbox" value="z" >3</td> 
      <td >Stack</td> 
      <td >Aya</td> 
     </tr> 
     </tbody> 
     </table> 

Aber Pech, das Array leer, was ist los?

+0

können Sie HTML-Teil der Tabelle ,, mit Form und Kontrollkästchen hinzufügen? –

+2

hast du es schon mal versucht? hier ist es https://stackoverflow.com/questions/11292778/use-jquery-to-get-values-of-selected-checkboxes –

+0

Sie haben Code in jquery gegeben, aber das Tag ist Javascript –

Antwort

2

Es wird Ihnen helfen

Ich habe geändert.

if ($(this).is(':checked')) { 
    } 

$("document").ready(function(){ 
 
$('#fpdId').click(function(){ 
 
    var files = new Array(); 
 

 
    //xzyId is table id. 
 
    $('#xzyId tbody tr input:checkbox').each(function() { 
 
     if ($(this).is(':checked')) { 
 
     files.push(this.value); 
 
     } 
 
    }); 
 

 
    console.log(files); 
 
}); 
 

 
}) 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table border=1px class="xzy" id="xzyId" style="width:100%"> 
 
    <colgroup> 
 
     <col style="width:10%"> 
 
     <col style="width:80%"> 
 
     <col style="width:10%"> 
 
    </colgroup> 
 
    <tbody> 
 
    <tr> 
 
     <td ><input type="checkbox" value="x" >1</td> 
 
     <td >Stack</td> 
 
     <td >John</td> 
 
    </tr> 
 
    <tr> 
 
     <td ><input type="checkbox" value="y" >2</td> 
 
     <td >Stack</td> 
 
     <td >Sansa</td> 
 
    </tr> 
 
    <tr> 
 
     <td ><input type="checkbox" value="z" >3</td> 
 
     <td >Stack</td> 
 
     <td >Aya</td> 
 
    </tr> 
 
    </tbody> 
 
    </table> 
 

 
<input type="button" id="fpdId" value="filePack">

+0

Dies ist jquery, das Tag ist Javascript –

+0

Yah! Ihr richtiges @ Omkaar.k aber innerhalb des Codes in obiger Frage verwendet jquery Recht –

Verwandte Themen