0

我目前有这个javascript:

$(document).on('click', '.add_option', function(e){
    e.preventDefault();
    $.get("/options/render_option", function(data){
        $('.add_option_control_group').before(data.option)
        $('#pollview').append(data.view)
        $(document).on('keyup', '.options', function(){
            that = $(this);
            index = $('.options').index(this);
            $('label.pollviewoptions:eq(' + index + ')').find('span').text(that.val());
        });
    }); 
});

HTML:

 <div class="span3">
        <label class="control-label" for="inputPhoto">Option</label>
        <div class="controls">
            <input class="span3 options" name="option[]" type="text" placeholder="Text">
            <br />
        </div>
    </div>
    <div class="span2">
        <label class="control-label" for="inputPhoto">Image</label>
        <div class="controls">
            <input class="span2" name="userfile[]" type="file" id="inputFile" placeholder="File">
            <br />
            <?php echo isset($file_errors['userfile']) ? "<span class='label label-important'>{$file_errors['userfile']}</span>" : '';?>
        </div>
    </div>
    <div class="clearfix">&nbsp;</div>
    <div class="span3">
        <label class="control-label" for="inputPhoto">Option</label>
        <div class="controls">
            <input class="span3 options" name="option[]" type="text" placeholder="Text">
            <br />
        </div>
    </div>
    <div class="span2">
        <label class="control-label" for="inputPhoto">Image</label>
        <div class="controls">
            <input class="span2" name="userfile[]" type="file" id="inputFile" placeholder="File">
            <br />
        </div>
    </div>
    <div class="clearfix">&nbsp;</div>        
<div class="add_option_control_group control-group">
    <label class="control-label" for="inputDescription">&nbsp;</label>
    <div class="controls">
        <a href="javascript:void(0)" class="btn btn-primary add_option">Add Option</a>
        <br />
    </div>
</div>

<div class="span4">
    <div class="well" id="pollview">
        <legend>Legend</legend>
        <label class="checkbox pollviewoptions">
            <input type="checkbox"><span>Check me out</span>
        </label>
        <label class="checkbox pollviewoptions">
            <input type="checkbox"><span>Check me out</span>
        </label>
    </div>
</div>

它目前不起作用。ajax 请求从响应中检索 html 并将元素插入页面的 2 个区域。

数据选项:

<div class="span3">
    <label class="control-label" for="inputPhoto">Option</label>
    <div class="controls">
        <input class="span3" name="option[]" type="text" placeholder="Text">
        <br />
    </div>
</div>
<div class="span2">
    <label class="control-label" for="inputPhoto">Image</label>
    <div class="controls">
        <input class="span2" name="userfile[]" type="file" id="inputFile" placeholder="File">
        <br />
        <?php echo isset($file_errors['userfile']) ? "<span class='label label-important'>{$file_errors['userfile']}</span>" : '';?></span>
    </div>
</div>
<div class="clearfix">&nbsp;</div>

数据视图

<label class="checkbox pollviewoptions">
    <input type="checkbox"> Check me out
</label>

创建的所有元素都不响应 keyup 事件。

4

1 回答 1

2

没有 class .optionsin 的文本输入元素data.option,只有.span3.

于 2012-10-13T03:11:51.030 回答