自动对焦在使用 MVC3 Razor 的部分视图内的表单中不起作用
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个代码:
Try this code: