zoukankan      html  css  js  c++  java
  • Javascript 判断数据绑定中的A列和B列拼接组成的新值不能重复

    主要代码如下:

    1. replace() 方法用于在字符串中用一些字符替换另一些字符。 

    2. substring() 方法用于提取字符串中介于两个指定下标之间的字符。

    3. split() 方法用于把一个字符串分割成字符串数组。

    4. join() 方法用于把数组中的所有元素放入一个字符串。

    5.indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置,如果要检索的字符串值没有出现,则该方法返回 -1。

    一. Javascript 代码:

    //Javascript 判定数据绑定中的服务单编号+SN不能重复
    var val = "";
    var strVal = "";
    function save() {
        var tr = document.getElementById("grdResult").getElementsByTagName("tr");
        if (tr.length < 2) return;
       for (var i = 1; i < tr.length; i++) {
          td = tr[i].getElementsByTagName("td");
          if (td[2].children[0].innerText != "" || td[3].children[0].defaultValue != "") {
                   val = td[2].children[0].innerText.replace(" ", "").replace(" ", "") + "$" 
                       + td[3].children[0].value.replace(" ", "").replace(" ", "") + "," + val;
                      strval = val.substring(0, val.length - 1);
           }

         }
           var arrs = strval.split(",");
           var arr = arrs.join(",") + ",";
          for (var i = 0; i < arrs.length; i++) {
          if (arr.replace(arrs[i] + ",", "").indexOf(arrs[i] + ",") > -1) { 
               alert("列表中存在服务单编号+SN重复" + arrs[i]);
              break;
      }
    }
        val = "";
        strVal = "";//注意这里需要赋值为空,因为二次提交的时候会把前面的数据也保存到数组里面。
    }

    二. HTML 代码:

    <div style="200px;"><input onclick="save();" style=" float:right;" type="button" value="保存" /></div>
    <TABLE style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; BORDER-COLLAPSE: collapse; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid" id=grdResult class="Grid" rules=all cellSpacing=0 cellPadding=0>
    <tr class="GridHeader">
    <td style="WIDTH: 20px" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: center; BORDER-LEFT: 0px; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=chkall title=all onclick="selectAllgetCheck('grdResult');" name=chkall value="" type=checkbox> </td>
    <td style="WIDTH: 200px; WHITE-SPACE: nowrap" align=center>部品代码</td>
    <td style="WIDTH: 200px; WHITE-SPACE: nowrap" align=center>服务单编号</td>
    <td style="WIDTH: 200px; WHITE-SPACE: nowrap" align=center>SN</td>
    <td style="WIDTH: 100px; WHITE-SPACE: nowrap" align=center>收货状态</td>
    <td style="WIDTH: 100px; WHITE-SPACE: nowrap" align=center>发货数量</td>
    <td class="hidden">&nbsp;</td>
    <td class="hidden">&nbsp;</td>
    <td class="hidden">&nbsp;</td></tr>
    <tr class="GridNormal" onmouseover="this.className='GridHover';" onmouseout="this.className='GridNormal';">
    <td align=center><INPUT style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; WIDTH: 100%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_chkl_0 name=grdResult$ctl02$chkl value=70c3e59f-f786-e211-a6dd-00155d002d65 type=checkbox> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_oldmaterialid_0 class="noemptyInput" disabled name=grdResult$ctl02$oldmaterialid value=键盘操作单元 height=25 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><A href="../services/servicesDetail.aspx?MasterID=fb4e1604-6b58-e211-91a2-00155d002d63">&nbsp;RP-2013010700002&nbsp;</A> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_oldmodulenum_0 class="noemptyInput" disabled name=grdResult$ctl02$oldmodulenum value=0LTCW20237965-000050 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_receiptstatus_0 class="noemptyInput" disabled name=grdResult$ctl02$receiptstatus value=未收货 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: right; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_setoutnum_0 class="noemptyInput" disabled name=grdResult$ctl02$setoutnum value=1 type=text> </td>
    <td class="hidden"><INPUT id=grdResult_reSysID_0 name=grdResult$ctl02$reSysID value=fb4e1604-6b58-e211-91a2-00155d002d63 type=text></td>
    <td class="hidden"><INPUT id=grdResult_reoldmateriaID_0 name=grdResult$ctl02$reoldmateriaID value=9b3bebf6-0b45-e211-ba39-00155d002f0d type=text></td>
    <td class="hidden"><INPUT id=grdResult_repairtype_0 name=grdResult$ctl02$repairtype type=text></td></tr>
    <tr class="GridNormal" onmouseover="this.className='GridHover';" onmouseout="this.className='GridNormal';">
    <td align=center><INPUT style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; WIDTH: 100%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_chkl_0 name=grdResult$ctl02$chkl value=70c3e59f-f786-e211-a6dd-00155d002d65 type=checkbox> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_oldmaterialid_0 class="noemptyInput" disabled name=grdResult$ctl02$oldmaterialid value=键盘操作单元 height=25 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><A href="../services/servicesDetail.aspx?MasterID=fb4e1604-6b58-e211-91a2-00155d002d63">&nbsp;RP-2013010700002&nbsp;</A> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_oldmodulenum_0 class="noemptyInput" disabled name=grdResult$ctl02$oldmodulenum value=0LTCW20237965-000050 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_receiptstatus_0 class="noemptyInput" disabled name=grdResult$ctl02$receiptstatus value=未收货 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: right; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=grdResult_setoutnum_0 class="noemptyInput" disabled name=grdResult$ctl02$setoutnum value=1 type=text> </td>
    <td class="hidden"><INPUT id=grdResult_reSysID_0 name=grdResult$ctl02$reSysID value=fb4e1604-6b58-e211-91a2-00155d002d63 type=text></td>
    <td class="hidden"><INPUT id=grdResult_reoldmateriaID_0 name=grdResult$ctl02$reoldmateriaID value=9b3bebf6-0b45-e211-ba39-00155d002f0d type=text></td>
    <td class="hidden"><INPUT id=grdResult_repairtype_0 name=grdResult$ctl02$repairtype type=text></td></tr>
    <tr class="GridNormal" onmouseover="this.className='GridHover';" onmouseout="this.className='GridNormal';">
    <td align=center><INPUT style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; WIDTH: 100%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=Checkbox1 name=grdResult$ctl02$chkl value=70c3e59f-f786-e211-a6dd-00155d002d65 type=checkbox> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=Text1 class="noemptyInput" disabled name=grdResult$ctl02$oldmaterialid value=键盘操作单元 height=25 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><A href="../services/servicesDetail.aspx?MasterID=fb4e1604-6b58-e211-91a2-00155d002d63">&nbsp;RP-2013010700001&nbsp;</A> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=Text2 class="noemptyInput" disabled name=grdResult$ctl02$oldmodulenum value=0LTCW20237965-000051 type=text onclick="return Text2_onclick()"> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: left; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=Text3 class="noemptyInput" disabled name=grdResult$ctl02$receiptstatus value=未收货 type=text> </td>
    <td style="WHITE-SPACE: nowrap" align=center><INPUT style="BORDER-BOTTOM: 0px; TEXT-ALIGN: right; BORDER-LEFT: 0px; WIDTH: 98%; BORDER-TOP: 0px; BORDER-RIGHT: 0px" id=Text4 class="noemptyInput" disabled name=grdResult$ctl02$setoutnum value=1 type=text> </td>
    <td class="hidden"><INPUT id=Text5 name=grdResult$ctl02$reSysID value=fb4e1604-6b58-e211-91a2-00155d002d63 type=text></td>
    <td class="hidden"><INPUT id=Text6 name=grdResult$ctl02$reoldmateriaID value=9b3bebf6-0b45-e211-ba39-00155d002f0d type=text></td>
    <td class="hidden"><INPUT id=Text7 name=grdResult$ctl02$repairtype type=text></td></tr>
    </TABLE>


    如图:

     

    JS判断一个数组中是否有重复值的三种方法: http://www.360doc.com/content/13/0321/19/6692427_273074433.shtml

     

  • 相关阅读:
    我爱Java系列之---【SpringBoot打成war包部署】
    279. Perfect Squares
    矩阵dfs--走回路
    112. Path Sum
    542. 01 Matrix
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    Invert Binary Tree
    563 Binary Tree Tilt
    145 Binary Tree Postorder Traversal
  • 原文地址:https://www.cnblogs.com/allenhua/p/3025378.html
Copyright © 2011-2022 走看看