开发者

jquery on the fly new element

Once a user click on a button it creates new input text on the fly.

Lets assume those 2 input are created on the fly.

<input id="txtImageSource_1" type="text">
<input id="txtImageSource_2" type="text">

Now to simplify my problem. I need to show the id of the input when they enter text inside it. Again those 2 elements have been created on the fly.

I tried the following but don't work.

    $('#' + $(this).attr('id')).live('keyup', function(){alert($(this).attr('id')));
                });

Creating the text box

$("#add_image").click(function() { 
var开发者_JS百科 i = ($('p.fields').length);
        $('<p class="fields" id="field_' + i + '"><input type="text" id="txtImageSource_' + i + '"/>"<input type="button" value="Delete" class="btnDelete" id="' + i + '"></p>').appendTo('#formelement');
  });   


$("#add_image").click(function()
{ 
    var i = ($('p.fields').length);

    $('<p class="fields" id="field_' + i + '"><input type="text" id="txtImageSource_' + i + '"/>"<input type="button" value="Delete" class="btnDelete" id="' + i + '"></p>').appendTo('#formelement');

    $("#txtImageSource_" + i).live('keyup', function()
    {
        // Do whatever you want to do

        alert( $(this).attr("id") );
    });
});


        $("#add_image").click(function () {
            var i = ($('p.fields').length);
            $('<p class="fields" id="field_' + i + '"><input class="img" type="text" id="txtImageSource_' + i + '"/><input type="button" value="Delete" class="btnDelete" id="' + i + '"/></p>').appendTo($('#formelement'));
            $('#txtImageSource_' + i ).live("keyup", function () {
                alert($(this).attr("id"));
            });
            return false;
        });  


I'm not sure where the first $(this) is coming from in this code:

$('#' + $(this).attr('id')).live('keyup', function(){alert($(this).attr('id')));
            });

But if it is from an onFocus or click binding, you can eliminate the rest of the selector like so:

$(this).live('keyup', function(){alert($(this).attr('id')));
            });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜