8

jQuery 1.7.0 を使用していますが、TypeError: $(...).attr(...) is undefined が引き続き発生します。誰かが私に何が問題なのか教えてもらえますか? 私のjQueryは以下です。ありがとうございました。

    function removeNewItem() {
        $('.aboutitemdelete').unbind('click');

        $('.aboutitemdelete').on('click', function () {

            // Get Related Object
            var current = $(this).attr('rel').replace('aboutitemremove_', '');

            // Removing Element
            $('#aboutitem_' + current).remove();

            //Assign Current Element Count
            answercount = parseInt($('#answercount').val());
            answercount -= (answercount > 1) ? 1 : 0;
            $('#answercount').val(answercount);
        });
    };

    $(document).ready(function () {
        $('.button_admin_newitem').on('click', function () {

            alert('a');

            current = $('.aboutsectionitem:last').attr('id').replace('aboutitem_', '');
            current = Number(++current);

            alert('b');

            $('div.aboutitemsholder').append('<div id="aboutitem_' + current + '" class="aboutsectionitem"><h4 class="titlebar_span">About Page Item</h4><div class="aboutrow_sep"><label>Section Label</label><input id="seclabel_' + current + '" type="text" class="text" /><div class="clear"></div></div><div class="aboutrow_sep"><label>Section Content</label><div class="aboutrow_editor_holder">Telerik Editor Goes Here<div class="clear"></div></div><div class="clear"></div></div><div class="aboutrow_sep"><label>Enabled?</label><input id="itemenable_' + current + '" type="checkbox" class="checkbox" /><div class="clear"></div></div><div class="aboutrow_sep"><label>Sort Order</label><input id="sortitem_' + current + '" type="text" class="text" style="max-width: 50px;" /><div class="clear"></div></div><div class="clear"></div><div class="aboutitemremove"><label>Delete</label><a href="#" class="aboutitemdelete" rel="aboutitemremove_' + current + '"><span>Remove</span></a></div></div>');

            answercount = parseInt($('#answercount').val()) + 1;
            $('#answercount').val(answercount);

            removeNewItem();

            return false;
        });
    });
4

1 に答える 1

17

http://api.jquery.com/attr/

jQuery 1.6 以降、.attr() メソッドは、設定されていない属性に対して undefined を返します。

私の推測では、検索している属性が、検索している要素に設定されていません。このため、 undefined には replace メソッドがないため、 .replace は失敗しています。最初に未定義でないことを確認する必要があります。

var current = $(this).attr('rel');
current = current ? current.replace('aboutitemremove_', '') : '';

次の領域の id に対してもこれを行います。

于 2013-08-14T19:12:51.877 に答える