【问题标题】:Autofocus does not work in form inside a partial view using MVC3 Razor自动对焦在使用 MVC3 Razor 的局部视图中不起作用
【发布时间】:2012-02-01 00:22:37
【问题描述】:

我正在使用 MVC 3 Razor,并且在 PartialView 中有一个简单的表单 [我将其显示为弹出窗口]。某些功能在 PartialView 中无法正常工作。例如自动对焦属性。

@Html.TextBoxFor(model => model.Code, new { style = "width:50px", maxlength = 4, autofocus = "" })

我已经尝试通过 jQuery 脚本设置焦点,但也不起作用。

$('#Code').focus();

我错过了什么吗?我是否必须在部分视图中导入一些脚本才能使其正常工作?

非常感谢任何帮助。

提前致谢。

更新:

我发现了问题...我正在加载窗口,然后在延迟 700 毫秒后显示它。我注释掉了 setTimeout 行并且它起作用了。一定是在加载表单时焦点起作用了,但是当我实际执行“open()”时,它又失去了焦点。

以前的代码

function openWindow(url, title) {
  var window = $("#Window").data("tWindow");
  window.ajaxRequest(url);
  window.title(title);
  setTimeout(showWindow, 700);
}

function showWindow() {
  var window = $("#Window").data("tWindow");
  window.open().center();
}

新代码

function openWindow(url, title) {
  var window = $("#Window").data("tWindow");
  window.ajaxRequest(url);
  window.title(title).open().center();;
//setTimeout(showWindow, 700);
}

感谢大家的回复。希望它可以帮助某人。

【问题讨论】:

  • 你弹窗里有JS吗?它是 iFrame 还是内联?
  • 我实际上没有任何脚本除了 $('#Code').focus();是的,我在 PartialView 本身中有这个脚本。我正在使用 Telerik 的窗口并从 Partial View 加载内容。
  • 检查你的js是否正在加载使用fiddler或firebug
  • 我也尝试过 firebug 的 $('#Code').focus() 。它不起作用。它不显示任何错误,只是返回输入对象。
  • 请检查您在弹窗中加载的JS库

标签: forms asp.net-mvc-3 razor asp.net-mvc-partialview autofocus


【解决方案1】:

试试这个代码:

function openWindow(url, title) {
  var window = $("#Window").data("tWindow");
  window.ajaxRequest(url);
  window.title(title).open().center();
  //call after the content has been loaded from the Ajax request
  document.getElementById('Code').focus();
}

【讨论】:

  • 嗨,保罗,我试过你的代码,但它说 focus() 是未定义的。
  • 我已经仔细检查了所有的 JS 文件。
  • 我需要查看您的代码。您是使用LoadContentFrom() 方法还是只是在Content() 方法中渲染@Html.Partial("view")
  • 很奇怪。下次我会推荐使用在这里找到的内置事件demos.telerik.com/aspnet-mvc/razor/window/clientsideevents 你可以调用onActivate 事件然后设置焦点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2012-03-12
  • 1970-01-01
相关资源
最近更新 更多