开发者

jQuery syntax / logic query

开发者 https://www.devze.com 2023-03-25 12:19 出处:网络
I\'ve put some code together which partially works and throws up no errors in Firebug. jQuery( document ).ready( function( $ ) {

I've put some code together which partially works and throws up no errors in Firebug.

jQuery( document ).ready( function( $ ) {

    var i = jQuery('[name="reference[]"]').size() + 1;

    jQuery('a#add_reference').click(function() {
        input_code = '<div class="reference">' +
            '<input type="hidden" value="" name="reference[]" />' +
            '<h3>Reference #' + i + '</h3>' +
            '<div class="line double">' +
                '<label>Author(s)</label> <input type="text" name="ref_authors" />' +
                '<label>Publisher</label> <input type="text" name="ref_publisher" />' +
            '</div>' +
            '<div class="line double">' +
                '<label>Publication Year</label> <input type="text" name="ref_pub_year" />' +
                '<label>Page Number(s)</label> <input type="text" name="ref_page_nums" />' +
            '</div>' +
            '<div class="line"><label>Document Title</label> <input type="text" name="ref_doc_title" /></div>' +
            '<div style="clear:both;">&nbsp;</div>' +
            '<p>Click <a href="javascript:void(0);" id="' + i + '_del_reference">here</a> to remove Reference #' + i + '.</p>' +
        '</div>';

        jQuery(input_code).appendTo('#references_wrapper');

        i++;
    });

    jQuery('[id="' + i + '_del_reference"]').click(removeBox);
    jQuery('[id="1_del_reference"]').click(removeBox);

    var removeBox = function() {
        jQuery( this ).parents('div.reference').remove();
        return false;
    };


});

Which accompanies the HTML:

<div id="references_wrapper">
        <p>Click <a href="javascript:void(0);" id="add_reference">here</a> to add a new reference.</p>
        <div class="reference">
            <input type="hidden" value="" name="reference[]" />
            <h3>Reference #1</h3>
            <div class="line double">
                <label>Author(s)</label> <input type="text" name="ref_authors" />
                <label>Publisher</label> <input type="text" name="ref_publisher" />
            </div>
            <div class="line double">
                <label>Publication Year</label> <input type="text" name="ref_pub_year" />
                <label>Page Number(s)</label> <input type="text" name="ref_page_nums" />
            </div>
            <div class="line"><label>Document Title</label> <input type="text" name="ref_doc_title" /></div>
            <div style="clear:both;">&nbsp;</div>
            <p>Click <a href="javascript:void(0);" id="1_del_reference">here</a> to remove Reference #1.</p>
        </div>
    </div>

THE PROBLEM is that I can add references but can't delete them.

Could someone please point out where I've gone wrong? Frustrating thing is I've done this kind of thing a few times (with slightly different eff开发者_Python百科ects) and I still can't get it working!

Thanks in advance.


Small logic error; you were incrementing the variable i without assigning the event handler (thus you couldnt remove the references) correctly. Changed it to .live() to accomodate new objects with event handlers.

Updated fiddle : http://jsfiddle.net/UZkjE/

0

精彩评论

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

关注公众号