MaskedEditExtender在执行规则方面做得很好,但是我的用户在键入其TextBox时遇到了麻烦。
当它获得焦点时,我想选择我的TextBox的所有内容。
常规的JavaScript解决方案不起作用。
onfocus="javascript:this.select();"
MaskedEditExtender会干扰。
当它获得焦点时,如何选择文本框的所有内容?
<asp:TextBox
ID="TextBoxPrice"
runat="server" />
<ajaxToolkit:MaskedEditExtender
ID="MaskedEditExtenderTextBoxPrice"
runat="server"
TargetControlID="TextBoxPrice"
Mask="9,999.99"
MaskType="Number"
MessageValidatorTip="False"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
InputDirection="RightToLeft"
AcceptNegative="None"
DisplayMoney="Left" />
<ajaxToolkit:MaskedEditValidator
ID="MaskedEditValidatorTextBoxPrice"
runat="server"
ControlToValidate="TextBoxPrice"
ControlExtender="MaskedEditExtenderTextBoxPrice"
Display="Dynamic"
IsValidEmpty="False"
EmptyValueMessage="Price is required"
InvalidValueMessage="Price is invalid"
MinimumValue= "0.01"
MinimumValueMessage="Price is too small"
MaximumValue="9999.99"
MaximumValueMessage="Price is too large" />
最佳答案
使用此脚本:
<script type="text/javascript">
function selectAllCharsBefore(inputText, char) {
setTimeout(function () {
if (!inputText) return false;
var indexChar = inputText.value.indexOf(char);
if (indexChar != -1) createSelection(inputText, 0, indexChar)
}, 100);
return true;
}
function whatDecimalSeparator() {
var n = 1.1;
n = n.toLocaleString().substring(1, 2);
return n;
}
function createSelection(field, start, end) {
if (field.createTextRange) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
field.focus();
} else if (field.setSelectionRange) {
field.focus();
field.setSelectionRange(start, end);
} else if (typeof field.selectionStart != 'undefined') {
field.selectionStart = start;
field.selectionEnd = end;
field.focus();
}
}
</script>
关于asp.net - MaskedEditExtender太难用了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1477590/