0

我有一个动态设置为表单中不同文本框的方法。但问题是我只希望该方法在我认为称为riskcore.cshtml 的情况下起作用。有办法吗?像 if(page == riskcore.cshtml){ do method} 之类的代码?

4

3 回答 3

2

您可以在 JavaScript 中测试 URL:

if(/\/riskscore\.chtml$/.test(window.location.pathname)) {
  // You're on riskscore.chtml... Do something
}
于 2013-07-30T06:57:18.483 回答
2

JavaScript 通常无法知道特定视图文件是否或何时在服务器端使用。它只知道视图呈现的结果。

您可以将视图的内容包装在 a<div class="riskscore">中,然后在其中选择文本框:

$('.riskscore :text')...

您还在评论中提到,如果没有此视图,其他元素将不存在。您可以将它们用作您的条件,检查它们是否存在:

if ($('.other-elements').length) {
    // do method
}

'.other-elements'根据需要更换。

于 2013-07-30T07:02:10.487 回答
1

您可以使用它window.location来获取当前位置。或者您可以使用location.pathname当前路径。

链接在这里

我认为这会有所帮助

于 2013-07-30T06:59:06.447 回答