可以't使用jQuery设置HTML

Can't set HTML using jQuery

本文关键字:jQuery 设置 HTML 使用 可以      更新时间:2023-09-26

由于某种原因,在删除textbox元素后,脚本没有写出文本。我是错误地使用了.html还是其他什么问题?

        $('.time').click(function () {
            var valueOnClick = $(this).html();
            $(this).empty();
            $(this).append("<input type='text' class='input timebox' />");
            $('.timebox').val(valueOnClick);
            $('.timebox').focus();
            $('.timebox').blur(function () {
                var newValue = $(this).val();
                var dataToPost = { timeValue: newValue };
                $(this).remove('.timebox');
                if (valueOnClick != newValue) {
                    $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                            alert(msg);
                            $(this).html("88");
                        }
                    });
                } else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                    alert("else");
                    $(this).html("88");
                }
            });
        });

好吧,多亏了这些评论,我发现我指的是错误的东西。对我来说,解决方案是更改模糊函数如下:

            $('.timebox').blur(function () {
                var newValue = $(this).val();
                var dataToPost = { timeValue: newValue };
                if (valueOnClick != newValue) {
                    $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                        }
                    });
                } else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                }
                $(this).parent().html("8");
                $(this).remove('.timebox');
            });

成功处理程序中的$(this)指的是msg,而不是$('.timebox')(或您想将html附加到的任何元素)

$(this) = '.timebox'  element but you have removed it already, 
  $.ajax({
                        type: "POST",
                        url: "Test",
                        data: dataToPost,
                        success: function (msg) {
                            alert(msg);
  $(this).html("88");  // This = msg
                        }
and 
 else {
                    // there is no need to send 
                    // an ajax call if the number
                    // did not change
                    alert("else");
                    $(this).html("88"); // this = '.timebox'  element but you have removed it already, 
                }

如果您输入一个函数,这个值就会更改。因此,当你在模糊函数处理程序中使用它时,它实际上指向".timebox"

    $('.time').click(function () {
        var valueOnClick = $(this).html();
        var $time=$(this);//If you want to access .time inside the function for blur
                          //Use $time instead of$(this)
        $(this).empty();
        $(this).append("<input type='text' class='input timebox' />");
        $('.timebox').val(valueOnClick);
        $('.timebox').focus();
        $('.timebox').blur(function () {
            var newValue = $(this).val();
            var dataToPost = { timeValue: newValue };
            $(this).remove(); //Since $(this) now refers to .timebox
            if (valueOnClick != newValue) {
                $.ajax({
                    type: "POST",
                    url: "Test",
                    data: dataToPost,
                    success: function (msg) {
                        alert(msg);
                        $(this).html("88");
                    }
                });
            } else {
                // there is no need to send 
                // an ajax call if the number
                // did not change
                alert("else");
                $(this).html("88");
            }
        });
    });