複数のテキストボックスの自動提案検索ボックスの基本を作ろうとしています。テキストボックスを追加/削除するスクリプトが既にあり、正常に動作しています。しかし、私が抱えている問題は、ページ上のすべてのテキストボックスを取得できることです.キーが押されると(基本的にonkeyup)、テキストボックスの値が警告されます. HTMLで自分自身を追加したテキストボックスでこれを機能させることができましたが、jqueryを使用して追加されたものは機能しません。
ここにjqueryがあります:
$(document).ready(function(){
$counter = 0; // initialize 0 for limitting textboxes
$('#buttonadd').click(function(){
if ($counter < 10)
{
$counter++;
$('#buttondiv').append('<div><label>Textbox #'+$counter+'</label><input type="text" name="textbox[]" class="textbox" value="" id="country"/></div>');
}else{
alert('You cannot add more than 10 textboxes');
}
});
$('#buttonremove').click(function(){
if ($counter){
$counter--;
$('#buttondiv .textbox:last').parent().remove(); // get the last textbox and parent for deleting the whole div
}else{
alert('No More textbox to remove');
}
});
$('#buttonget').click(function(){
alert($('.textbox').serialize()); // use serialize to get the value of textbox
});
$('input').bind('keyup', function() {
alert($(this).val());
});
$('#dropdownadd').change(function(){
$('#dropdowndiv').html(""); // when the dropdown change set the div to empty
$loopcount = $(this).val(); // get the selected value
for (var i = 1; i <= $loopcount; i++)
{
$('#dropdowndiv').append('<div><label>Textbox #'+i+'</label><input type="text" name="textbox2[]" class="textbox2" value="" /></div>');
}
});
});
ここにhtmlがあります:
<div id="buttondiv">
<!-- this is where textbox will appear -->
</div>
<div class="choices">
<span>Adding Textbox using Button</span>
<input type="button" id="buttonadd" value="Add Textbox"/>
<input type="button" id="buttonremove" value="Remove Textbox"/>
<input type="button" id="buttonget" value="Get Textbox" />
</div>
<input id="country" size="25" type="text" />