Jquery事件不适用于动态创建的html

时间:2011-08-26 04:54:15

标签: jquery

<li>
        <label>Chapter Title</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>    

表单字段和javascript代码是动态创建的。

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
            });
        }
    });     

图片上传效果很好,缩略图显示但标题字段没有显示,也没有显示错误。

3 个答案:

答案 0 :(得分:0)

change

中更改图像来源后,您可能会绑定到$('#image_src2').val(response)处理程序

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

尝试使用加载回调:这将触发并在满载时显示图像

    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();
        });
    }
相关问题