开发者

Jquery event does not work on dynamically created html

开发者 https://www.devze.com 2023-03-30 17:33 出处:网络
<li> <label>Chapter Title<开发者_StackOverflow社区;/label> <input type=\"text\" name=\"chapter[1][title]\" />
<li>
        <label>Chapter Title<开发者_StackOverflow社区;/label>
        <input type="text" name="chapter[1][title]" />
    </li>

    <li>    
        <label>Text</label>
        <textarea name="chapter[1][text]" ></textarea>
    </li>
    <li>
        <input type="file" name="image2" id="image2" />
        <img id="thumb2" width="100px" height="100px"/>
        <input type="hidden" id="image_src2" name="chapter[1][photo]" />
    </li>

    <li id="caption2" style="display:none;">
        <label>Photo Caption</label>
        <input type="text" name="chapter[1][photo_caption]" />
    </li>    

The form fields and javascript code is created dynamically.

var thumb = $('img#thumb2'); 
    new AjaxUpload('image2', {
        action: "action",
        name: 'userfile',
        onSubmit: function(file, extension) {
            $('div.preview').addClass('loading');
        },
        onComplete: function(file, response) {
            thumb.load(function(){
                $('div.preview').removeClass('loading');
                thumb.unbind();
            });
            thumb.attr('src', response);
            $('#image_src2').val(response);
            $('#image_src2').live('change',function()
            {
                $('#caption2').show(); // this does not work
            });
        }
    });     

the image is uploading well and the thumbnail is shown but the caption field does not show up and no error is shown.


Try this:

$('#image_src2').val(response);
$('#caption2').show();


You may be binding to the change handler after you change the image source in $('#image_src2').val(response)


Try using the load callback: this will fire as and display the the image when it is fully loaded

    onComplete: function(file, response) {
        thumb.load(function(){
            $('div.preview').removeClass('loading');
            thumb.unbind();
        });
        thumb.attr('src', response);
        $('#image_src2').val(response);
        $('#image_src2').load(function()  // changed to "load"
        {
            $('#caption2').show();
        });
    }
0

精彩评论

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