$("#myinputfield").focus();
Doing that on an input field with it's value already set causes the current value to be selected. How can I focus the field without selecting the text?
在已经设置了其值的输入字段上执行此操作将导致当前值被选中。如何在不选择文本的情况下聚焦字段?
7 个解决方案
#1
27
I've searched around a bit and kinda found out that you should re-set your value into the desired field after it is focussed on.
我已经搜索了一些,并发现您应该将您的值重新设置为所关注的字段。
#2
26
How about something like this where you set the value of the input field to itself after focusing it?
像这样,你将输入字段的值设置为它的焦点之后如何?
$("#myinputfield").focus().val($("#myinputfield").val());
#3
6
Other answers suggest setting the value of the input field to itself after focusing it. If in Firefox, the cursor appears at the start of the input and you want it at the end, then a slight modification is required. You will have to set the value as empty first and then set it to the previous value.
其他的答案建议将输入字段的值设置为自己的焦点。如果在Firefox中,光标出现在输入的开头,而您希望它在结尾,那么需要稍微修改一下。您必须先将值设置为empty,然后将其设置为先前的值。
var value = $("#myinputfield").val();
$("#myinputfield").focus().val('').val(value);
#4
3
Have you tried this?
你有试过吗?
var txt = $("#myinputfield").val();
$("#myinputfield").focus();
$("#myinputfield").val( txt );
#5
0
function focusField(id){
var inputField = document.getElementById(id);
if (inputField != null && inputField.value.length != 0){
if (inputField.createTextRange){
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character',inputField.value.length);
FieldRange.collapse();
FieldRange.select();
}else if (inputField.selectionStart || inputField.selectionStart == '0') {
var elemLen = inputField.value.length;
inputField.selectionStart = elemLen;
inputField.selectionEnd = elemLen;
inputField.focus();
}
}else{
inputField.focus();
}
}
#6
0
$(document).ready(function() {
var $field = $("#formloginusername"),
oldVal = $field.val();
$field.focus().val('').val(oldVal);
});
DEMO: http://jsfiddle.net/X7Y8S/
演示:http://jsfiddle.net/X7Y8S/
i took the code & demo from ahren's answer in other topic, i think it's helpful.
我从ahren的答案中得到了代码和演示,我认为这很有帮助。
#7
0
This solution works very well for me:
这个解决方案对我来说非常有效:
$("#myinputfield").focus(function (event) {
var value = $("#myinputfield").val();
setTimeout(function() {
$("#myinputfield").val(value);
},10);
});
#1
27
I've searched around a bit and kinda found out that you should re-set your value into the desired field after it is focussed on.
我已经搜索了一些,并发现您应该将您的值重新设置为所关注的字段。
#2
26
How about something like this where you set the value of the input field to itself after focusing it?
像这样,你将输入字段的值设置为它的焦点之后如何?
$("#myinputfield").focus().val($("#myinputfield").val());
#3
6
Other answers suggest setting the value of the input field to itself after focusing it. If in Firefox, the cursor appears at the start of the input and you want it at the end, then a slight modification is required. You will have to set the value as empty first and then set it to the previous value.
其他的答案建议将输入字段的值设置为自己的焦点。如果在Firefox中,光标出现在输入的开头,而您希望它在结尾,那么需要稍微修改一下。您必须先将值设置为empty,然后将其设置为先前的值。
var value = $("#myinputfield").val();
$("#myinputfield").focus().val('').val(value);
#4
3
Have you tried this?
你有试过吗?
var txt = $("#myinputfield").val();
$("#myinputfield").focus();
$("#myinputfield").val( txt );
#5
0
function focusField(id){
var inputField = document.getElementById(id);
if (inputField != null && inputField.value.length != 0){
if (inputField.createTextRange){
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character',inputField.value.length);
FieldRange.collapse();
FieldRange.select();
}else if (inputField.selectionStart || inputField.selectionStart == '0') {
var elemLen = inputField.value.length;
inputField.selectionStart = elemLen;
inputField.selectionEnd = elemLen;
inputField.focus();
}
}else{
inputField.focus();
}
}
#6
0
$(document).ready(function() {
var $field = $("#formloginusername"),
oldVal = $field.val();
$field.focus().val('').val(oldVal);
});
DEMO: http://jsfiddle.net/X7Y8S/
演示:http://jsfiddle.net/X7Y8S/
i took the code & demo from ahren's answer in other topic, i think it's helpful.
我从ahren的答案中得到了代码和演示,我认为这很有帮助。
#7
0
This solution works very well for me:
这个解决方案对我来说非常有效:
$("#myinputfield").focus(function (event) {
var value = $("#myinputfield").val();
setTimeout(function() {
$("#myinputfield").val(value);
},10);
});