1

我有一个像 Virtual Team Resources 这样的标题,然后我需要它的短代码,比如 VTR 如果 Title Virtual Team 那么 ShortCOde 将是 VTE 我已经实现了这个,但仍然存在一些问题,比如如果用户输入标题 Virtual T 那么短代码会?

我的代码------

function EnterShortCode() {
           debugger
           var ProjectShortCode = "";
           var Arr = $("#txtProjectTitle").val().rtrim().split(" ");
           for (i = 0; i < Arr.length; i++) {
               if (Arr[i] != "" && Arr[i] != null) {
                   ProjectShortCode += Arr[i].substring(0, 1);
                   if (i == Arr.length - 1) {
                       if (ProjectShortCode.length == 1) {
                           if (Arr[i] < 3) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 3);
                           }
                       }
                       else if (ProjectShortCode.length == 2) {
                           if (Arr[i] < 2) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 2)
                           }

                       }

                   }
               }
           }

           ProjectShortCode = ProjectShortCode.toUpperCase();
           alert(ProjectShortCode);
       }
4

1 回答 1

0

我已经稍微修改了你的功能,这可能会对你有所帮助

 function EnterShortCode() {
     var ProjectShortCode = "";
           var Arr = rtrim($("#txtProjectTitle").val()).split(" ");
    if (Arr.length>2)
    {        
    for (i = 0; i < Arr.length; i++) {
               if (Arr[i] != "" && Arr[i] != null) {
                   ProjectShortCode += Arr[i].substring(0, 1);
                   if (i == Arr.length - 1) {
                       if (ProjectShortCode.length == 1) {
                           if (Arr[i] < 3) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 3);
                           }
                       }
                       else if (ProjectShortCode.length == 2) {
                           if (Arr[i] < 2) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 2)
                           }

                       }

                   }
               }
           }
         }
       else
       {
          for (i = 0; i < Arr.length; i++) {
               if (Arr[i] != "" && Arr[i] != null) {
                             if(i==0 && Arr.length==2)
              {              
                 ProjectShortCode += Arr[i].substring(0, 2);
              }
                           else if(i==0 && Arr.length==1)
                           {
                               ProjectShortCode += Arr[i].substring(0, 3);
                           }
              else
              {
                     ProjectShortCode += Arr[i].substring(0, 1);
              }   

                   if (i == Arr.length) {
                       if (ProjectShortCode.length == 1) {
                           if (Arr[i] < 3) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 3);
                           }
                       }
                       else if (ProjectShortCode.length == 2) {
                           if (Arr[i] < 2) {

                           }
                           else {
                               ProjectShortCode = ProjectShortCode + Arr[i].substring(1, 2)
                           }

                       }

                   }
               }
           }                         
       }
           ProjectShortCode = ProjectShortCode.toUpperCase();
           alert(ProjectShortCode);
    }

现场演示

于 2012-11-20T06:43:13.303 回答