簡體   English   中英

javascript無法調用未定義的方法“替換”

[英]javascript Cannot call method 'replace' of undefined

$(document).ready(function() 
{
    var oldText, newText;
    $(".editable").hover(
        function()
        {
            $(this).addClass("editHover");
        }, 
        function()
        {
            $(this).removeClass("editHover");
        }
    );

    $(".editable").bind("dblclick", replaceHTML);


    $(".btnSave").live("click", 
                    function()
                    {
                        newText = $(this).siblings("form")
                                         .children(".editBox")
                                         .val().replace(/"/g, """);

                        currentId = $(this).parent().attr("id");
                        $.ajax({
                        type:   "POST",
                        url:    "./musa_date_edit_ajax.php",
                        data:   "vidpubdate="+newText+"&id=" + currentId,
                        success: function(msg){
                            //alert("test ok");
                        } 
                        });

                        $(this).parent()
                               .html(newText)
                               .removeClass("noPad")
                               .bind("dblclick", replaceHTML);
                    }
                    ); 

    $(".btnDiscard").live("click", 
                    function()
                    {
                        $(this).parent()
                               .html(oldText)
                               .removeClass("noPad")
                               .bind("dblclick", replaceHTML);
                    }
                    ); 

    function replaceHTML()
                    {
                        oldText = $(this).html()
                                         .replace(/"/g, """);
                        $(this).addClass("noPad")
                               .html("")
                               .html("<form><input type=\"text\" class=\"editBox\" value=\"" + oldText + "\" /> </form><button type=\"button\" class=\"btnSave\" >planifier</button> <!-- <button type=\"button\" class=\"btnDiscard\" >Annuler</button> -->")
                               .unbind('dblclick', replaceHTML);

                    }
}
); 

IE / Chrome說:
無法調用未定義的方法“替換”

參考下面的代碼:

                newText = $(this).siblings("form")
                                 .children(".editBox")
                                 .val().replace(/"/g, "&quot;");

奇怪的是,它在Firefox上可以正常工作。
我想這與似乎未定義的$ this有關。 但是由於我不是JS / Jquery專家,所以我不知道如何在Chrome和IE上修復它。

附錄:
HTML大致如下:

<form>
    blabla
    <table>
        <tr>
            <td>blabla</td>
            <td class="editable" id="<?php echo $data['id']; ?>" ><?php echo $data['date'];?></td>
        </tr>
    </table>
</form>

更新:

<form>
    <table>
        <td>some useless stuff</td>
        <td class="editable noPad" id="1732">
            <form>
                <input class="editBox" value="22/05/13" type="text">
            </form>
            <button type="button" class="btnSave">planifier</button>
            <!-- <button type="button" class="btnDiscard">Annuler</button> -->
        </td>
    </table>
</form>

可能只是縮短的html代碼段中的錯字,但您使用的是html中的“ editable”類和javascript中的“ editbox”類。

$('.editbox')返回空,因此.val()返回未定義,因此replace()導致異常

要弄清楚父母/兄弟姐妹的事情是否正確,您需要在html中顯示更多細節

編輯:怎么樣:

$(this).parent().find('.editBox').val().replace(...

嘗試這個:

$(this).closest("td").find(".editBox").val().replace(/"/g, "&quot;");

另外,一個建議。 您可以將當前代碼最小化:

$(".editable").hover(
function () {
    $(this).addClass("editHover");
},
function () {
    $(this).removeClass("editHover");
});

通過做這個:

$(".editable").hover(
function () {
    $(this).toggleClass("editHover");
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM