자바스크립트 input 값 합치기 도움 부탁 드려요~

자바스크립트 input 값 합치기 도움 부탁 드려요~

QA

자바스크립트 input 값 합치기 도움 부탁 드려요~

답변 3

본문

자바스크립트로 여러 input의 값을 합쳐서 한개의 input 값에 넣으려고 아래처럼 구현 하였습니다.

sell1~sell6까지 총 6개의 input 의 값을 sell7 input에 넣고 있는데요.

아래처럼 구현하면 작동은 되지만 경우의 수도 많고 코드도 너무 길어지는데

간단하게 구현 할 방법이 없을까요? 도움 부탁 드립니다~

 

<input type='text' name='stx1' id='sell1' onkeyup='call()' value='<?=stripslashes($stx1)?>'>
<input type='text' name='stx2' id='sell2' onkeyup='call()' value='<?=stripslashes($stx2)?>'>
<input type='text' name='stx3' id='sell3' onkeyup='call()' value='<?=stripslashes($stx3)?>'>
<input type='text' name='stx4' id='sell4' onkeyup='call()' value='<?=stripslashes($stx4)?>'>
<input type='text' name='stx5' id='sell5' onkeyup='call()' value='<?=stripslashes($stx5)?>'>
<input type='text' name='stx6' id='sell6' onkeyup='call()' value='<?=stripslashes($stx6)?>'>
<input type='hidden' name='stx' id='sell7' onkeyup='call()' value='<?=stripslashes($stx)?>'>

 

<script language='javascript'>
function call()
{

 if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;

 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell4").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell3").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;

 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell3').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell3").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell3').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell4").value && document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell4').value + " " + document.getElementById('sell5').value + " " + document.getElementById('sell6').value;

 }else if(document.getElementById("sell1").value && document.getElementById("sell2").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell2').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell3").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell3').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell1").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell3").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell3').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell2").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell4').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell3").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell4").value && document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell4').value + " " + document.getElementById('sell5').value;
 }else if(document.getElementById("sell4").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell4').value + " " + document.getElementById('sell6').value;
 }else if(document.getElementById("sell5").value && document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell5').value + " " + document.getElementById('sell6').value;

 }else if(document.getElementById("sell1").value){
  document.getElementById('sell7').value = document.getElementById('sell1').value;
 }else if(document.getElementById("sell2").value){
  document.getElementById('sell7').value = document.getElementById('sell2').value;
 }else if(document.getElementById("sell3").value){
  document.getElementById('sell7').value = document.getElementById('sell3').value;
 }else if(document.getElementById("sell4").value){
  document.getElementById('sell7').value = document.getElementById('sell4').value;
 }else if(document.getElementById("sell5").value){
  document.getElementById('sell7').value = document.getElementById('sell5').value;
 }else if(document.getElementById("sell6").value){
  document.getElementById('sell7').value = document.getElementById('sell6').value;
 }

}
</script>

이 질문에 댓글 쓰기 :

답변 3


$(function() {
    var total = 0;
    $("input[name^='stx']:visible").on("keyup", function() {
        $(this).map(function() {
            return total += parseInt($(this).val());
        });
        $("#sell7").val(total);
    });
});

위는 덧셈을 한 총합이고 단순히 문자열로 나열만 하실 거면


$(function() {
    $("input").on("keyup", function() {
        var str = $("input[name^='stx']:visible").map(function() {
            return $(this).val();
        }).get().join(" ");
        $("#sell7").val(str);
    });
});

이렇게 하시면 될 것 같네요.

function call() {
    var str = "";
    $('input[type=text][name^=stx]').each(function(idx) {
        if (idx == 0)
            str = $(this).val();
        else
            str = str + " " + $(this).val();
    });
    
    $('#sell7').val(str);
}

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 947
© SIRSOFT
현재 페이지 제일 처음으로