jquery auto appending text
my aim is that there is a textbox , 1)In the textbox the domain is already written. 2) When i enter 开发者_StackOverflow社区some text in the textbox the domain should append to it like
domain=abc.com
text written = c://inetpub/cms/
the textbox should have c://inetpub/cms/abc.com with abc.com auto appended. abc.com should not be editable
You should attach an onchange-eventhandler to the textbox. Then compare the last seven characters, and if they're not "abc.com", add that string at the end.
$('#textbox').change(function() {
if ($(this).val().substr(-7, 7) != 'abc.com') {
$(this).val($(this).val() + 'abc.com');
}
});
Edit: Possibly the onchange-event may interfere with the user input. In that case, use the blur() event.
Also, if the length of the 'abc.com' string may differ, you can probably save 'abc.com' in a variable and use (-variable.length, variable.length) to select the substring.
And if you don't want the user to be able to edit the 'abc.com' part, remove it on focus() event, and re-add it on blur() event.
In my opinion , you want to design this textbox like facebook textboxes.
Here are some links about it
http://www.rorcraft.com/2008/8/13/the-facebook-autocomplete-address-to-field
http://wharsojo.wordpress.com/2008/02/18/jquery-facebook-autocomplete
http://github.com/loopj/jQuery-Tokenizing-Autocomplete-Plugin/
Also you develop your code like this. You define a variable in your javascript code. Its first value is 0. And if textbox append str then you change your variable with 1. In your textboxChange function you check firslty your variable is 0 or 1.
精彩评论