This Code uses jquery to dynamically Add a limited amount of text, clear the text content, and use the after () method to append elements. The specific implementation is as follows. For more information, see, I hope it will be helpful to you. This Code uses jquery to dynamically Add a limited amount of text (five in this example), clear the text content, and use the after () method to append elements.
The Code is as follows:
Script
$ (Document). ready (function (){
Var spotMax = 5; // limit the total number of text elements added
Var afterId = 0; // id of the element to be appended
Var newId = 1; // the id of the new text
If ($ ('table # vote'). size ()> = spotMax ){}
$ ("# BtnAddOption"). click (function (){
AfterId ++;
NewId = afterId + 1;
AddSpot (this, spotMax, afterId, newId );
});
});
// Add Option Method
Function addSpot (obj, sm, afterId, newId ){
If ($ ('tr. spot '). size () <sm ){
$ ('# Vote _' + afterId). after (
''+ AfterId +''+
''+
'');
$ (": Text [id ^ = 'txtinput _ ']"). val ("input text..."); // assign the initial value to the new text
}
Else {
Alert ("up to five votes can be added! ");
}
};
// Reset Options
$ ("Input # btnResetOption"). click (function (){
$ (": Text [id ^ = 'txtinput _ ']"). val (""); // clear text content
});
Script
The Code is as follows:
The running effect is as follows: