aspmvc2 使用 Spark 引擎使用 jquery 渲染局部视图

发布于 2024-09-26 20:41:11 字数 1445 浏览 0 评论 0原文

我正在尝试返回用 Spark 渲染的 asp mvc2 中的部分视图。 该视图包含一些 JavaScript。 一切都被渲染,但脚本不执行,

这里是代码

<table>
<tr>
<td> 
    !{Html.LabelFor(model => model.SourceId, new { @class = "label-inline", title = "Source d'ouvrage" })}
    !{Html.DropDownList("SourceList", Model.SourceList, "Choisir la source", new { @class = "combo-box" })}
</td>
<td>
    !{Html.LabelFor(model => model.AgentId, new { @class = "label-inline", title = "Représentant" })}
    !{Html.DropDownList("AgentList", Model.AgentList, "Choisir le représentant", new { @class = "combo-box" })}
</td>
</tr>

<script type="text/javascript">
$("#SourceList").change(function() {
    alert('youpi');
    $.ajaxSetup({ cache: false });
        var selectedItem = $(this).val();
        alert(selectedItem);
        if (selectedItem == "" || selectedItem == 0) {
            //Do nothing or hide...?
        } else {
            var path = '~/sources/' + selectedItem + '/agents';
            $.getJSON(path, function(data) {
                agents = data;
                var items = "";
                $.each(data, function(i, agents) {
                    items += "<option value='" + agents.Id + "'>" + agents.Name + "</option>";
                });
                $("#AgentList").html(items);
            });
        }
    });
</script>

我做错了什么? 谢谢

I'm trying to return a partial view in asp mvc2 rendered with spark.
The view contains some javascript.
Everything gets rendered but the script does not execute

here is the code

<table>
<tr>
<td> 
    !{Html.LabelFor(model => model.SourceId, new { @class = "label-inline", title = "Source d'ouvrage" })}
    !{Html.DropDownList("SourceList", Model.SourceList, "Choisir la source", new { @class = "combo-box" })}
</td>
<td>
    !{Html.LabelFor(model => model.AgentId, new { @class = "label-inline", title = "Représentant" })}
    !{Html.DropDownList("AgentList", Model.AgentList, "Choisir le représentant", new { @class = "combo-box" })}
</td>
</tr>

<script type="text/javascript">
$("#SourceList").change(function() {
    alert('youpi');
    $.ajaxSetup({ cache: false });
        var selectedItem = $(this).val();
        alert(selectedItem);
        if (selectedItem == "" || selectedItem == 0) {
            //Do nothing or hide...?
        } else {
            var path = '~/sources/' + selectedItem + '/agents';
            $.getJSON(path, function(data) {
                agents = data;
                var items = "";
                $.each(data, function(i, agents) {
                    items += "<option value='" + agents.Id + "'>" + agents.Name + "</option>";
                });
                $("#AgentList").html(items);
            });
        }
    });
</script>

What i'm i doing wrong?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-10-03 20:41:11

Jquery Ajax 函数将从结果中剥离 JavaScript 并在将 HTML 插入 DOM 之前运行它。

您的两个最佳选择是

将 JS 包装在视图的非 ajax 部分(视图或同时渲染的部分)上的函数中,并调用此函数作为 ajax 调用上的成功回调

或替换 < code>$("#SourceList").change(function() { 与 $("#SourceList").live('change', function() { 即使这样做您最好不要将其包含为通过 Ajax 返回的部分。

Jquery Ajax functions will strip the javascript out from the result and run it before inserting the HTML into the DOM.

Your two best options are either

Wrap the JS in a function on the non-ajax part of the view (either the view or a partial that's rendered at the same time) and call this function as your success callback on your ajax call

Or replace $("#SourceList").change(function() { with $("#SourceList").live('change', function() { though even doing this you're still better off not including it as the partial returned via Ajax.

如梦初醒的夏天 2024-10-03 20:41:11

我会将脚本放入 jQuery 文档就绪事件处理程序中,以便在 DOM 完全加载后运行。可能是脚本在 DOM 创建您的 #SourceList 之前运行。

前任:

<script type="text/javascript">
    $(function() {
        $("#SourceList").change(function() {
            ....

I would put the script inside the jQuery document ready event handler so that it gets run AFTER the DOM has fully loaded. it could be the script is running before the DOM creates your #SourceList.

ex:

<script type="text/javascript">
    $(function() {
        $("#SourceList").change(function() {
            ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文