MVC 部分视图和不显眼的 jQuery/Javascript
我正在努力弄清楚应该如何编写使用 jQuery 的部分视图。
例如,在下面的部分视图中,我显示了一个连接到 jquery 自动完成插件的文本框。
最终,我希望结果旁边显示一个“X”,用户可以将其删除,但就本示例而言,所选文本只是附加到同级 div 中。
我的 jQuery 代码可以放在部分视图中吗,还是应该将其放在外部文件中?如果使用外部文件,那么使用分部视图的另一个开发人员将需要知道应该包含外部 js 文件 - 这样可以吗?
我想我的问题确实是,虽然我对 jQuery 很满意(在 DOM 中查找元素、异步调用操作等),但我需要一些指导来告诉我在哪里放置所有这些代码,这将阻止我得到一片混乱。
我刚刚输入的内容有意义吗???
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$(".acEmployee").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"],
select: function(event, ui) {
$(this).siblings().append(ui.item.value);
}
});
});
</script>
<div><div></div><input class="acEmployee" /></div>
I'm struggling to work out how I should be authoring my partial views which use jQuery.
For example, in the partial view below, I am displaying a text box which is hooked up to the jquery autocomplete plugin.
Eventually I want the results to be shown with an "X" next to them which the user can then remove, but for the purpose of this example the selected text is simply appended to the sibling div.
Is my jQuery code OK sitting here in the partial view, or should I be putting it in an external file? If an external file is used, then another developer using the partial view will need to be aware that the external js file should be included - is that OK?
I suppose my question really is that, although I'm kind of OK with jQuery (finding elements in the DOM, calling actions asynchronously etc..), I need some sort of guidance on where to put all this code that will stop me getting in a mess.
Does any of what I have just typed make sense????
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$(".acEmployee").autocomplete({
source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"],
select: function(event, ui) {
$(this).siblings().append(ui.item.value);
}
});
});
</script>
<div><div></div><input class="acEmployee" /></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 JavaScript 是“连接”代码,那么我不介意它与部分代码一起出现 - 毕竟,它相关的所有内容都在那里。
如果 JavaScript 是“框架”代码并且可以在任何地方重用,并且不依赖于部分中的任何内容,那么我将其放在自己的文件中。
这就是我所做的,但我也有兴趣听听其他人的方法。
If the JavaScript is "hookup" code then I don't mind it being in with the partial - as after all, everything it relates to is there.
If the JavaScript is "framework" code and can be reused all over the place, and isn't dependant on anything in the partial then I put it in its own file.
That's just what I do, but I'd be interested in hearing other peoples methods too.
如果可能,请将所有 js 代码保存在外部 js 文件中。这样你就可以把你的 jquery 包含在页面的底部。
对于 .net mvc,请尝试以下操作:
然后,您可以将特定于页面的内容放入内容占位符中。 ,plugins.js 中的母版页特定插件,您可以使用
var path = MVC_baseurl + "Controller/Action"
并在 ajax 调用等中使用它...
If possible, keep all your js code in an external js files. That way you can put your jquery includes at the bottom of the page.
for .net mvc try something like:
You can then put your page-specific stuff in the content placeholders. , your master page specific plugins in plugins.js, and you can use
var path = MVC_baseurl + "Controller/Action"
and use that in your ajax calls etc...