2016-06-27 1 views
0

このコードは、すべてのテキストボックスに値0の最後のテキストボックスにフォーカスしてダイナミックテキストボックスを作成します。ここで作成した動的jqueryを使用して最後に作成したダイナミックテキストボックスのフォーカスに動的に値を追加

<html> 
<head> 
<script> src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script> 
<script> 
$(document).ready(function(){ 

$("input").on('focus' , addNew); 

function addNew(){ 
    if($(this).is(':last')){ 
    $(this).after('<input type="text" value="0"/>'); 
    $("input").on('focus' , addNew); 
    } 
} 
}); 
</script> 
</head> 
<body> 
<script> 

    var divBox1 = document.createElement("div"); 
    divBox1.style.height = "100%"; 
    divBox1.style.width = "200px"; 

    document.body.appendChild(divBox1) 
    var input = document.createElement('input'); 
    divBox1.appendChild(input); 

</script> 
</body> 
</html> 
+0

..だから問題は何ですか? –

+0

動的値のソースは何ですか? –

+0

http://stackoverflow.com/users/4417952/keyur-shah http://stackoverflow.com/users/3639582/shaunak-d ....私はそれを感謝した – Harinath

答えて

0

いくつかの問題:

<script>src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script> 
されている必要があります

inputlastであるかどうかを確認する方法は適切ではありませんでした。下記の更新スニペットを確認してください。

var divBox1 = document.createElement("div"); 
 
divBox1.className = "ipHolder"; 
 
//adding a class to identify the container of input elements 
 

 
divBox1.style.height = "100%"; 
 
divBox1.style.width = "200px"; 
 

 
document.body.appendChild(divBox1) 
 
var input = document.createElement('input'); 
 
divBox1.appendChild(input); 
 

 
$(document).ready(function() { 
 
    $("input").on('focus', addNew); 
 

 
    function addNew() { 
 
    var isLast = $(this).closest('.ipHolder').find('input').length - ($(this).index() + 1) == 0; 
 
    //subtract the number of inputs present already with the index of clicked input 
 
    //within in the container for inputs and see if it is 0 which means its the last element 
 
    
 
    if (isLast) { 
 
     $(this).after('<input type="text" value="0"/>'); 
 
     $("input").on('focus', addNew); 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> 
 
</script>

関連する問題