繁体   English   中英

jQuery变量未使用replaceWith更新

[英]jQuery Variable is Not Updating with replaceWith

我正在尝试将我的DOM替换为变量。 但是当我的ajax运行时,它没有为变量分配新值。

知道为什么不替换变量值吗? 如下所示,当我写出DOM Id时,我可以工作,但是当我将其替换为变量时,则无法工作。

这是我压缩的代码有效

$(document).ready(){
    var url = '/Home/GetBookingsTicker';
    $('#button').on('click',function(){
        $.ajax({
            url: url,
            cache: false,
             type:"GET",
             success: function (response) {
                 $('#marqueeTop').replaceWith(response);
                 setColors($('#one').val());
             },
             failure: function () {
                 alert("Failed to update!")
             }
         });
     }
}

代码不起作用

$(document).ready(){
    var url = '/Home/GetBookingsTicker';
    var $one = $('#one');
    $('#button').on('click',function(){
        $.ajax({
            url: url,
            cache: false,
             type:"GET",
             success: function (response) {
                 $('#marqueeTop').replaceWith(response);
                 setColors($one.val());
             },
             failure: function () {
                 alert("Failed to update!")
             }
         });
     }
}

剃刀

<div id="marqueeTop">
    @Html.HiddenFor(m => m.one)
    <span>
        @foreach(var item in Model.procBookings){
            <label class="setTopColors">
                @Html.DisplayFor(x => item.GroupName)
                @Html.DisplayFor(x => item.Sales).....
            </label>   
        }
    </span>
</div>

第二个块函数无权访问变量$ one。 您可能需要阅读异步AJAX请求的工作方式。 要访问$ one,您将需要成功实现回调。

如果我对#one假设是正确的,这可能会起作用:

$(document).ready(){
var url = '/Home/GetBookingsTicker';

var one = $('#one').val();
$('#button').on('click',function(){
    $.ajax({
        url: url,
        cache: false,
         type:"GET",
         success: function (response) {
             $('#marqueeTop').replaceWith(response);
             setColors(one);
         },
         failure: function () {
             alert("Failed to update!")
         }
     });
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM