开发者

Programatically select portion inside an text input field

开发者 https://www.devze.com 2023-02-24 08:54 出处:网络
I have a common <input type=\"text\" id=\"foo\" /> I can select all text on click by $(\"#foo\").select();

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号