[英]Setting value via Javascript sets focus to textbox in IE10
I have the following Textbox 我有以下文本框
<asp:TextBox runat="server" ID="txtSeleID" Columns="12" MaxLength="14"
onfocus="javascript:SetSearchText();"
onchange="javascript:SetSearchText();"></asp:TextBox>
<asp:RegularExpressionValidator id="REV_SeleID" runat="server"
ErrorMessage="ID must contain up to 12 numbers."
Display="Dynamic" ControlToValidate="txtSeleID"
ValidationExpression="ID...|^\s*(\d{1,12}|\d{4}-\d{4}|\d{3}-\d{4}|\d{2}-\d{4}|\d{1}-\d{4}|\d{1}-\d{4}-\d{4}|\d{2}-\d{4}-\d{4}|\d{3}-\d{4}-\d{4}|\d{4}-\d{4}-\d{4})\s*$" CssClass="ValidationError" SetFocusOnError="True">(!)
</asp:RegularExpressionValidator>
and the below javascript : 和下面的javascript:
function SetSearchText() {
var Tbox = document.getElementById("<%=txtSeleID.ClientID%>");
if (Tbox.value == "ID...") {
Tbox.value = "";
Tbox.className = "";
}
}
$(document).ready(function () {
var Tbox = document.getElementById("<%=txtSeleID.ClientID%>");
if (Tbox.value.length == 0) {
Tbox.className = "quickEnter";
Tbox.value = "ID...";
}
if (Tbox.value == "ID...")
Tbox.className = "quickEnter";
});
Here is the button: 这是按钮:
<asp:ImageButton EnableViewState="false" id="SubmitBtn" runat="server"
ImageUrl='<%#Page.ResolveUrl("~/images/btn_submit.gif")%>'
onclick="Submit_Click" ToolTip="Submit" />
The issue is that when I click on my submit button, it calls the SetSearchText()
function and sets the value to ""
. 问题是,当我单击提交按钮时,它将调用
SetSearchText()
函数并将其值设置为""
。 In IE 10, it sets the focus to this textbox and I have to hit my submit button again to get it to submit. 在IE 10中,它将焦点设置到此文本框,我必须再次单击“提交”按钮才能将其提交。
If I comment out the setting of the value, the focus issue doesn't occur. 如果我注释掉该值的设置,则不会发生焦点问题。
How can I prevent the browser from forcing a focus to my textbox when attempting to submit? 尝试提交时,如何防止浏览器将焦点强加到我的文本框中?
EDIT: Added the regular expression validator to the code section. 编辑:将正则表达式验证器添加到代码部分。
EDIT: I found this issue that was having a similar issue, but I don't see a resolution. 编辑:我发现这个问题有一个类似的问题,但我看不到解决方案。 In this one, he is using html tags.
在这一本书中,他正在使用html标签。 I'm using ASP tags so I can't as easily change the input type.
我正在使用ASP标记,因此无法轻松更改输入类型。
I ended up doing a workaround by checking for the browser version. 我最终通过检查浏览器版本来进行解决。 If IE 10/11, I don't add the text to the input field.
如果是IE 10/11,则不会将文本添加到输入字段中。
$(document).ready(function () {
var lexidTbox = document.getElementById("<%=txtSeleID.ClientID%>");
//workaround for IE10/11 issue
var showLegalText = true;
// IF THE BROWSER IS IE10
if (navigator.appVersion.indexOf("MSIE 10") !== -1) {
showLegalText = false;
}
// IF THE BROWSER IS IE11
if (navigator.userAgent.indexOf("Trident") !== -1 && navigator.userAgent.indexOf("rv:11") !== -1)
{
showLegalText = false;
}
if (showLegalText) {
if (lexidTbox.value.length == 0) {
lexidTbox.className = "quickEnter";
lexidTbox.value = "ID...";
}
if (lexidTbox.value == "ID...")
lexidTbox.className = "quickEnter";
}
}); });
To be sure, I do not care for this workaround because it's dependent on checking IE versions, but this is the only way I could find to solve this issue. 可以肯定的是,我不关心这种解决方法,因为它依赖于检查IE版本,但这是我可以找到解决此问题的唯一方法。 This prevents the line
Tbox.value = "";
这样可以防止行
Tbox.value = "";
from being run, which avoids the issue I'm having. 从运行,避免了我遇到的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.