html部分:
<form method="post" id="myForm" action="ss" name="form2">
<input type="text" autofocus>
</form>
javascript部分:
function selectText(textbox, startIndex, stopIndex) {
if (textbox.setSelectionRange) {
textbox.setSelectionRange(startIndex, stopIndex);
} else if (textbox.createTextRange) {
var range = textbox.createTextRange();
range.collapse(true);
range.moveStart("character", startIndex);
range.moveEnd("character", stopIndex - startIndex);
range.select();
}
textbox.focus();
}
document.getElementById('myForm').elements[0].value = "12345678"
selectText(document.getElementById('myForm').elements[0],0,2)