自动对焦在使用 MVC3 Razor 的部分视图内的表单中不起作用

发布于 2024-12-29 22:25:38 字数 1048 浏览 1 评论 0原文

我正在使用 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);
}

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

I am using MVC 3 Razor and I have a simple form in a PartialView [I am showing this as a popup window]. Some of the features are not working properly in PartialView. For example the autofocus attribute.

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

I have tried setting the focus through jQuery script but that doesn't work either.

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

Am I missing something? Do I have to import some scripts in my partial view for this to work?

Any help is greatly appreciated.

Thanks in Advance.

Update:

I found the problem... I was loading the window and then showing it after a delay of 700ms. I commented out the setTimeout line and it worked. It must be that the focus worked when form loaded but when I actually did the "open()" it lost focus again.

Previous Code

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();
}

New Code

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

Thanks everyone for your replies. Hope it helps someone.

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

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

发布评论

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

评论(1

树深时见影 2025-01-05 22:25:38

试试这个代码:

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();
}

Try this code:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文