In the project, we need to create a checkbox keyword to prevent repeated features. The idea of using JS is as follows:
1. Get the text of all checkpoints and put them in the array.
2. Input a keyword loop array and use indexof to judge
Note: special characters may appear in the text of the checkbox, so special characters should be removed.
VaRStr= "Keywrod ";
VaRCh =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/;VaRSymbol =/([. * +? ^ $ {} () | [\] \/\])/;For(VaRI = 0; I <Str. length; I ++){VaRC =Str. charat (I );If(Ch. Test (c )){
Alert (C );}
If (symbol. Test (c )){
Alert (c)
}}
------------------------------- Split line -----------------------
CompleteCodeAs shown in the following figure (loop judgment in the Code is a bit more, and my Js capabilities are limited. I hope someone can provide other solutions or methods ):
FunctionCheckkeywords (){VaRKeywordsarray =[];//Checkboxlist nameVaRObjarray = ['industrialcatalogue ', 'businesscatalogue', 'industrialcatalogue']; Keywordsarray=Getallkeywords (objarray, keywordsarray );VaRKeyword;
VaRCompanycataloguedesc = $ ("# companycataloguedesc"). Val (). tolowercase (); // get the input content
VaR Ch =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/;
VaR Symbol =/([. * +? ^ $ {} () | [\] \/\] [0-9])/;
VaR En =/[A-Z]/;
VaR Descstr = "";
For ( VaR I = 0; I <companycataloguedesc. length; I ++ ){
C = Companycataloguedesc. charat (I );
If ((! Symbol. Test (c) & Ch. Test (c) | En. Test (c )){
Descstr = descstr + C;
}
}
Descstr = descstr. touppercase ();
For ( VaR I = 0; I <keywordsarray. length; I ++ ){
If (Keywordsarray [I]! = Undefined ){
Keyword = Keywordsarray [I];
If (Descstr. indexof (keyword)>-1 ){
Alert ( "The category is repeated. Please select from the above" );
$ ( "# Keytext" ). Focus ();
Return ;
}
}
}
}
// Obtain all Keywords of a page Element Function Getallkeywords (objarray, keywordsarray ){ VaR Keyword; For ( VaR I = 0; I <objarray. length; I ++ ) {$ ( "Input [name = '" + objarray [I] + "']"). Each (Function () {Keyword = $ ("Label [for = '" + $ ( This ). ATTR ('id') + "']" ). Text (); keywordsarray. Push (getchildkeyword (keyword, keywordsarray ));})} Return Keywordsarray ;} // Obtains the keywords separated by special characters in an element. Function Getchildkeyword (keyword, keywordsarray ){ VaR S2 = "" ; VaR Ch =/[\ u4e00-\ u9fa5 \ uf900-\ ufa2d]/ ; VaR Symbol =/([. * +? ^ $ {} () | [\] \/\])/ ; For ( VaR I = 0; I <keyword. length; I ++ ){ VaR C = Keyword. charat (I ); If (Symbol. Test (c )&&! Ch. Test (c) {S2 = Keyword. substr (0 , I); keyword = Keyword. substr (I + 1, Keyword. Length ); If (S2! = "" ) {Keywordsarray. Push (getchildkeyword (keyword, keywordsarray ));}} Else {S2 = S2 + C ;}} If (S2! = "" ){ Return S2 ;}}
------------------------------- Split line -----------------------
Originally, we wanted to use Lucene in the background to use Word Segmentation for judgment. However, Lucene was unfamiliar with it and the customer urged it. Therefore, we used Js. Try Lucene if you have time.
Modification description:
filter out the characters and numbers in the retrieved input content companycataloguedesc, and then match the keywords. Code comparison, which can be reconstructed. Do not copy + C/V directly, and apply it after reconstruction