我的部分视图中的 Jquery 或 Javascript 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5888343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:41:11  来源:igfitidea点击:

Jquery or Javascript problem in my partial view

c#javascriptjqueryasp.net-mvc

提问by user300485

Can I use jQuery or JavaScript code in partial views?

我可以在部分视图中使用 jQuery 或 JavaScript 代码吗?

I have a grid in my partial view and I am trying to hide one grid element using jQuery in that partial view. I am not able to do so. But same code works if I use it without a partial view.

我的局部视图中有一个网格,我试图在该局部视图中使用 jQuery 隐藏一个网格元素。我不能这样做。但是如果我在没有局部视图的情况下使用它,相同的代码就可以工作。

Can anybody help me out?

有人可以帮我吗?

Here is my code

这是我的代码

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<NascoBenefitBuilder.ViewModels.Obn.ProductTemplate.ObnProductTemplateMainData>" %>
<script type = "text/javascript" language="javascript">
$(document).ready(function(){
    alert("success");
});
</script>

This code is in my partil view but when this page loads I am not able to popup this alert box.

此代码在我的部分视图中,但是当此页面加载时,我无法弹出此警报框。

thanks Thanks

谢谢,谢谢

回答by Priyank

There could be few reasons for this not working.

这可能有几个原因不起作用。

Just to make sure your JQuery is available to partial view, you can try adding jquery references in your partial view page. Hoping you don't have similar issue like this one http://forums.asp.net/t/1649526.aspx/1

为了确保您的 JQuery 可用于局部视图,您可以尝试在局部视图页面中添加 jquery 引用。希望你没有类似的问题http://forums.asp.net/t/1649526.aspx/1

Second, if you are calling it through AJAX, javascript included in that partial view is not run by MVC AJAX. Follow this guideline to have it work for you.

其次,如果您通过 AJAX 调用它,则该部分视图中包含的 javascript 不会由 MVC AJAX 运行。遵循本指南,让它为您服务。

http://geekswithblogs.net/DougLampe/archive/2010/11/12/execute-javascript-in-mvc-partial-view.aspx

http://geekswithblogs.net/DougLampe/archive/2010/11/12/execute-javascript-in-mvc-partial-view.aspx

回答by ariel

Check the output page for nested tags, or if any markup is being escaped out. Also, check the browser's javascript console for errors.

检查输出页面是否有嵌套标签,或者是否有任何标记被转义。另外,检查浏览器的 javascript 控制台是否有错误。

回答by Gary Green

It might be a conflict or the character $ being interpreted incorrectly, try doing:

可能是冲突或字符 $ 被错误解释,请尝试执行以下操作:

<script type="text/javascript">
jQuery(document).ready(function(){
    alert("DOM loaded!");
});
alert("this script tag is executing properly!");
</script>