2017-12-11 2 views
0

내 프로젝트에서 배열에 체크 박스 값을 저장하고 싶습니다. 여기 체크 박스 값을 가져와 배열에 저장하는 방법

내 코드입니다 :

$('#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"> 

내 HTML 테이블은 세 개의 행이 각 행은 세 TDS 여기

를 가지고 테이블 코드 :

<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> 

그러나 불운은, 배열이는 빈, 무엇이 잘못 되었나요?

+0

당신은 형태와 체크 박스에 ,, 그 테이블의 HTML 부분을 추가 할 수 있습니까? –

+2

이미 검색을 시도 했습니까? 여기 https://stackoverflow.com/questions/11292778/use-jquery-to-get-values-of-selected-checkboxes –

+0

jquery에 코드를 입력했지만 태그는 javascript –

답변

2

당신에게 도움이 될 것입니다.

변경했습니다.

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

입니다. jquery입니다. 태그는 다음과 같습니다. javascript –

+0

예! 귀하의 올바른 @ Omkaar.k하지만 위의 질문에 코드의 jquery 권리를 사용하고 있습니다 –

관련 문제