2016-12-26 2 views
0

텍스트 상자 값을 다른 배열에 저장하려고합니다. 내 jQuery 코드 배열에서 textA 'textA'및 배열 텍스트 상자 값을 저장합니다. 'textB'값을 저장해야합니다. 결과에서 textB에는 텍스트 상자 'textA'의 값이 포함되어 있습니다. 결과적으로, 배열의 텍스트 상자 'textB'의 값만 원한다. textB.jQuery에서 텍스트 상자 값을 배열로 사용 하시겠습니까?

$(document).ready(function(){ 
 

 
    textA = textB = []; 
 
    
 
    $('button').click(function(){ 
 
    $('input[name^=textA]').each(function(){ 
 
    textA.push($(this).val()); 
 
    $('span#textA').text(textA); 
 
    }); 
 
    
 
    $('input[name^=textB]').each(function(){ 
 
    textB.push($(this).val()); 
 
    $('span#textB').text(textB); 
 
    }); 
 
textA = textB = []; 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" name="textA" placeholder="Enter text A"> 
 
<input type="text" name="textA" placeholder="Enter text A"></br> 
 
<input type="text" name="textB" placeholder="Enter text B"> 
 
<input type="text" name="textB" placeholder="Enter text B"></br> 
 
<button> 
 
Click 
 
</button></br> 
 
<span id="textA"></span></br> 
 
<span id="textB"></span>

답변

5

할당로서 별도로 textA = []; textB = [];

$(document).ready(function(){ 
 

 
    textA = []; 
 
    textB = []; 
 
    
 
    $('button').click(function(){ 
 
    $('input[name^=textA]').each(function(){ 
 
    textA.push($(this).val()); 
 
    $('span#textA').text(textA); 
 
    }); 
 
    
 
    $('input[name^=textB]').each(function(){ 
 
    textB.push($(this).val()); 
 
    $('span#textB').text(textB); 
 
    }); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" name="textA" placeholder="Enter text A"> 
 
<input type="text" name="textA" placeholder="Enter text A"></br> 
 
<input type="text" name="textB" placeholder="Enter text B"> 
 
<input type="text" name="textB" placeholder="Enter text B"></br> 
 
<button> 
 
Click 
 
</button></br> 
 
<span id="textA"></span></br> 
 
<span id="textB"></span>

관련 문제