【问题标题】:Run jquery code every time partial view is rendered with ajax每次使用 ajax 渲染局部视图时运行 jquery 代码
【发布时间】:2014-05-08 07:37:09
【问题描述】:

我正在使用 Ajax.Actionlink 在 MVC 中呈现局部视图。我也在使用 Modernizr datepicker,所以 firefox 可以和输入 type=date 的 Chrome 一样好。

当我单击我的第一个 Ajax 操作链接时,日期选择器工作正常。但是当我再次点击它时,它根本不起作用。

如果我重新加载页面 (F5),它会再次工作,第一次,而不是第二次和以后。

如何解决这个问题,让 jQuery 代码每次都运行,即使我切换到另一个局部视图?

我的 JS:

<script>
    $.noConflict();  
    if (!Modernizr.inputtypes.date) {
        $('input[type=date]').datepicker({ dateFormat: 'yy-mm-dd' });
    }
</script>

编辑:我尝试将 JS 包装在 $(function () {...});并且代码在局部视图内。也尝试使用 OnSuccess 选项,但结果是一样的。它只运行第一次,之后我需要重新加载页面

编辑 2:

我的 Ajax 操作链接:

<div id="createCalcMenu">
@Ajax.ActionLink("Annuity", "Annuity", "Calculation", new AjaxOptions
        {
             UpdateTargetId = "calcDiv",
             InsertionMode = InsertionMode.Replace,
             HttpMethod = "POST"
        })
                    <br />              
@Ajax.ActionLink("Amortization", "Amortization", "Calculation", new AjaxOptions
        {
             UpdateTargetId = "calcDiv",
             InsertionMode = InsertionMode.Replace,
             HttpMethod = "POST"
        })
</div>
    <div id="calcDiv"></div>

编辑 3:

我的部分观点:

@using (Ajax.BeginForm("ShowDetailAnnuity", "Calculation", new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "CalcDetail",
    LoadingElementId = "Loader"
}))
{
<input type="date" id="StartDate" name="startdate" title="Please enter start date (YYYY-mm-dd)" class="startDateTextbox" required />

//Rest of the form here...

<input type="submit" id="calcBtn" class="calcBtn" name="SubmitValue" value="Calculate" title="Calculate your calculation" />
}


<script>
$(function () {
    $.noConflict();
    if (!Modernizr.inputtypes.date) {
        $('input[type=date]').datepicker({ dateFormat: 'yy-mm-dd' });
    }
});
</script>

【问题讨论】:

  • 将此脚本放在局部视图中或将此脚本添加到Ajax.ActionLinkOnSuccess
  • 脚本在局部视图内
  • 你正在使用 jQuery,所以这不是在$(function(){...}); doc ready 块中。
  • @Jai 与我的代码结果相同(尝试过)
  • 我还尝试将 OnSuccess = "alert('blabla')" 添加到操作链接中,但它(就像 jquery 代码一样)仅在第一次运行。然后我需要重新加载页面才能再次显示

标签: jquery ajax asp.net-mvc datepicker


【解决方案1】:

我认为问题可能出在 jQuery 上

我复制了您的代码,只是在局部视图的脚本中做了一些小改动。 当我第一次运行代码时,它给了我一个错误,说 $ 未定义(尽管我已将 jQuery 库包含到主布局中)。 我在部分布局中手动调用了 jQuery,它起作用了。希望对你有帮助。

以下是我在年金部分视图代码中所做的更改:

<script>
    jQuery(function ($) {
          $.noConflict();
          if (!Modernizr.inputtypes.date) {
              $('input[type=date]').datepicker({ dateFormat: 'yy-mm-dd' });
          }
    });
</script>

无论我点击链接多少次,日期选择器总是为我而来。还要确保你对 JS 文件 jquery 和 mordernizr 的引用只有一次(在主布局中)

【讨论】:

  • 尝试了同样的结果。实在想不通为什么。也许有冲突或其他问题
  • 您如何在主题中调用您的脚本?我的意思是问是在 jQuery 之后调用了 mordernizr 吗?
  • 是的,modernizr 是在之后调用的
  • 我想我可能知道你为什么会得到这个。您是否也在局部视图中包含了 jQuery js 文件?如果你有,那么你应该删除部分视图中的引用(jquery 和 mordernizr),并在主布局中只调用一次,然后使用我提到的脚本。
  • 我只在我的主布局视图中链接了 jquery 和 modernizr
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 2011-11-17
  • 1970-01-01
  • 2023-03-30
  • 2010-10-19
  • 2016-01-03
  • 1970-01-01
相关资源
最近更新 更多