2016-08-02 3 views
0

처음에 사용자가 무언가를 입력하거나 다른 텍스트 상자/선택 상자를 생성 할 때 하나의 텍스트 상자/선택 상자가 생기며 생성 할 수있는 텍스트 상자/선택 상자의 수를 제한 할 수 있습니다.사용자가 무언가를 입력하면 동적으로 텍스트 상자를 생성합니다.

내가이

+1

사용 '으로 .Append()' – guradio

+0

확인하고 난 사용자가 아무것도 입력 여부를 확인할 수있는 방법? –

답변

2

당신은 동적으로 HTML을 생성하는 jQuery를 append() 방법을 사용할 수 있습니다 달성 할 수있는 방법을 나에게 조언을 주시기 바랍니다.

$("#ddlOptions").on("change", function(e) { 
 

 
    var len = $('.txtInput').length; 
 
    if (len == 0) { 
 
    $('.container').append('<br> <input type="text" id=txtTextBox_' + len + ' class="txtInput" /> <br>'); 
 
    } 
 
}); 
 

 
$(document).on("change", '.txtInput', function() { 
 
    var len = $('.txtInput').length; 
 
    
 
    if ($(this).val() != "") { 
 
    $('.container').append('<br> <input type="text" id=txtTextBox_' + len + ' class="txtInput" /> <br>'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <select id="ddlOptions"> 
 
    <option>option 1</option> 
 
    <option>option 2</option> 
 
    <option>option 3</option> 
 
    <option>option 4</option> 
 
    </select> 
 
</div>

+0

첫 번째 텍스트 상자가 채워진 경우 다른 하나를 채우는 경우 다른 하나를 채우는 경우 하나씩 추가하고 싶습니다. –

+0

@ websmentor.com 스 니펫을 확인하십시오. 코드가 업데이트되어 문제가 발생할 경우 알려주십시오. :) –

+0

괜찮 았어 고맙습니다. –

0

var counter=0; 
 
function generate(){ 
 
    var newTxt='<input type="text">'; 
 
    var newSel='<select><option value="">--select--</option></select>'; 
 
    var txtlimit=document.getElementById('txtlimit'); 
 
    var div=document.getElementById('newCtrls'); 
 
    var totalElem=div.getElementsByTagName('input').length; 
 
    
 
    if(!isNaN(txtlimit.value) && txtlimit.value!=''){ 
 
    if(parseInt(txtlimit.value)>counter && 
 
     parseInt(txtlimit.value)>totalElem){ 
 
     div.innerHTML += newTxt + newSel+'<br>'; 
 
     counter++; 
 
    } 
 
    }else{ 
 
    div.innerHTML +=newTxt + newSel+'<br>'; 
 
    } 
 
}
<input type="text" id="txtlimit" placeholder="Enter Limit"><br> 
 
<input type="text" onkeyup="generate();"> 
 
<select onchange="generate();"> 
 
    <option value="Item1">--select--</option> 
 
    <option value="Item1">Item1</option> 
 
    <option value="Item2">Item2</option> 
 
    <option value="Item3">Item3</option> 
 
</select><br> 
 
<div id="newCtrls"> 
 
    <h3>Dynamic Controls Here</h3> 
 
</div>

관련 문제