The input length dynamically changes with the input content. The input cursor locates at the rightmost, and the input cursor

Source: Internet
Author: User

The input length dynamically changes with the input content. The input cursor locates at the rightmost, and the input cursor

<Input type = "text" onkeydown = "this. onkeyup ();" onkeyup = "this. size = (this. value. length> 4? This. value. length: 4); "size =" 4 ">

<Input type = "text"> the default size is 20.

If you define the width attribute in the style and want its width to be changed according to the content, there will be no conflict between the two requirements. Therefore, the width attribute cannot be defined in the style.

Function len (s) {// get the length of the input text, one character, two Chinese characters
Var l = 0;
Var a = s. split ("");
For (var I = 0; I <a. length; I ++ ){
If (a [I]. charCodeAt (0) <299 ){
L ++;
} Else {
L + = 2;
}
}
Return l;
}
Or replace Chinese with two characters.
var str_temp = $(".tag_input").val().replace(/[\u4e00-\u9fa5]/g, 'aa');
$(".tag_input").attr("size",str_temp.length);
$(".tag_input").width("auto");

Jq:

$(function(){

                // Propertychange listens for character changes in input and attribute change events                 $( '.zy-price' ).bind( 'input propertychange' function () {                     var  $ this  = $( this );                     console.log($ this );                     var  text_length = $ this .val().length; // Obtain the length of the current text box                     var  current_width = parseInt(text_length) *16; // This value is to change the width before dividing by the length of the current string, and calculate the length of each character.                     console.log(current_width)                     $ this .css( "width" ,current_width+ "px" );                 });             })Position the input cursor in the last position: Method: assign a new value to the input.
$(".tag_input").focus();
$(".tag_input").val('');
$(".tag_input").val($(this).text());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.