`
yaozhiqiang109
  • 浏览: 117815 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

web前端界面多对象js验证

阅读更多

当一个表单要多对象提交时,界面的js验证时相当让人烦的事

如果事先有准备的话就会有事半功倍的效果,下面是一个freemarker模板

 <@s.form id="channel_form" name="channel_form" action="" method="post"  theme="simple">
  <table width="682" border="0" cellpadding="0" cellspacing="0">

  <script type="text/javascript">
          function validate_channel(elementValue,elementName){
                <!--elementValue循环的index   elementName 表示每个对象的名称-->
                <!--描述-->
                <!--构造id-->
                var channelDescription="#channelDescription";
                <!--注意:当在js中数字跟字符相拼接时,数字首先要转行成字符才行 -->
                channelDescription+=(elementValue).toString();
                <!--描述的值-->
                var channelDescriptionValue=$(channelDescription).val().replace(/^\s+|\s+$/g, "");
                var channelDescriptionLength=channelDescriptionValue.replace(/[^\x00-\xff]/g, 'xx').length;
                <!--当获取到元素的值和长度时,就可以在界面进行控制和提示了-->
                
                <!-- 数量-->
                var quantity="#quantity";
                quantity+=(elementValue).toString();
                var quantityValue=$(quantity).val();
                <!--跟描述同理-->
            return true;
        }
  </script>
   <#if channelList?exists>
       <#list channelList as channel>
        <#assign index=channel_index/>
  <tr>
    <td height="30">
    <table width="98%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="3%" height="30">&nbsp;</td>
        <td width="15%">描述</td>
        <td width="82%"><@s.textfield name="channelList[${index}].channelDescription" id="channelDescription${index}" size=47/></td>
      </tr>
      <tr>
        <td height="30">&nbsp;</td>
        <td height="30">分配库存量</td>
        <td height="30"><@s.textfield name="channelList[${index}].quantity" id="quantity${index}" /></td>
      </tr>
    </table></td>
  </tr>
    </#list>
   </#if>
  <tr>
    <td height="20" colspan="2" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" colspan="2" align="center"><label>
     <input type="button" value="保存" class="but02" onclick="return commit_channel_form();"/>
    </label></td>
  </tr>
</table>
</@s.form> 
 

 

 

注意:如果要把Number转换成String,可以使用Number的toString()方法
(1).toString()括号必须或者 1 .toString()空格必须

1
7
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics