Programatically select portion inside an text input field
I have a common <input type="text" id="foo" />
I can select all text on click by $("#foo").select();
The field does already contain a number (e.g. 25.40
)
How can I select only the 开发者_StackOverflow社区25
part?
For cross-browser support, you could use my Rangy Inputs jQuery plug-in for this, which will also work on textareas: http://code.google.com/p/rangyinputs/
With it, you can select everything before the decimal point as follows:
var $foo = $("#foo");
$foo.focus();
$foo.setSelection(0, $foo.val().indexOf("."));
jsFiddle example: http://jsfiddle.net/3XkJE/2/
var split = $("#foo").val().split(".");
var result = split[0];
精彩评论