当前位置: 代码迷 >> Java Web开发 >> function脚本有异常
  详细解决方案

function脚本有异常

热度:259   发布时间:2016-04-16 21:44:47.0
function脚本有错误
想问一下为什呢   validateFileType();中单击按钮不好使
$(document).ready(function(){
  var reg = /^\d+\.?\d*$/;
     $("#error-floorage").hide();
     $("#error-price").hide();
     $("#floorage").keyup(function(){
        if(reg.test($("#floorage").val())){
           $("#error-floorage").hide();
        }else{
           $("#error-floorage").show();
        }
  });
  $("#price").keyup(function(){
     if(reg.test($("#price").val())){
        $("#error-price").hide();
     }else{
        $("#error-price").show();
     }
  });
  });
  function validate(){
  validateFileType();
  if($("#floorage").val()=='' || reg.test($("#floorage").val())){
      $("#error-floorage").hide();
  }else{
      $("#error-floorage").show();
      return false;
  }
  return true;
  }
  function validateFileType(obj){
      var fileValue = obj !=null ? obj.value : document.getElementById("img").value;
      if(fileValue!=null && fileValue.length>0){
         var suffix = fileValue.substring(fileValue.length -3,fileValue.length);
         if(!(suffix=="jpg"||suffix=="JPG"||suffix=="gif"||suffix=="GIF")){
         alert("请选择jpg格式或gif格式的图片");
         return false;
         }else{
          document.getElementById("but").submit();
         }
         
         }
      }
  
  
  </script>
  <body>
      <div id="regLogin" class="wrap">
         <div class="dialog"> 
            <dl class="clearfix">
               <dt>&nbsp;发布信息</dt>
                 <dd class="past">填写信息</dd>
            </dl>
         <div class="box">
            <s:form action="doAddHouse.action" name="addHouse" id="addHouse" onsubmit="return validate();" enctype="multipart/form-data">
            <div class="infos">
              <table class="field">
                <tr>
                  <td class="field">标题</td>
                    <td>
                      <s:textfield cssClass="text" key="title" name="title" />
                    </td>
                 </tr>
                 <tr>
                    <td class="field">户型:</td>
                    <td>
                    <s:select list="typeList" name="HouseTypeID" listKey="id" listValue="name">
                    </s:select>
                </td>
                </tr>   
                <tr>
                   <td class="field">面积</td>
                 <td>
                 <s:textfield cssClass="text" key="floorage" id="floorage" name="fllorage" />
                 <span id="error-floorage"><font color="red">只能输入数字</font></span>
                </td>
                </tr>
            <tr>
              <td class="field">价格</td>
              <td>
                  <s:textfield cssClass="text" key="price" id="price" name="prices" />
                  <span id="error-price"><font color="red">只能输入数字</font></span>
              </td>
            </tr>   
            <tr>
               <td class="field">房产证日期</td>
            <td>
               <ss:datetimepicker id="datePicker" cssClass="text" name="puDate"></ss:datetimepicker>
               </td>
            </tr>
            <tr>
               <td class="field">位置</td>
               <td>
                 <div class="nobr"></div>
                 <s:doubleselect list="disList" doubleList="streetMap.get(top.id)"
                   name="districtId" doubleName="house.street.id" listKey="id" listValue="name" doubleListKey="id" doubleListValue="name">
                   </s:doubleselect>
               </td>
            </tr>
            <tr>
                <td class="filed">上传图片</td>
                <td>
                   <s:file id="img" name="img" cssClass="FileSty" onchange="validateFileType(this)" cssStyle="width:280">
                   </s:file><br/>
                <font color="red"><s:fielderror fieldName="img"/></font>
                   </td>
                   </tr>
           <tr>
              <td class="field">图片标题</td>
              <td>
                 <s:textfield name="upLoadFiletitle" cssClass="text" ></s:textfield>
              </td>
              </tr>
              <tr>
                <td class="field">已上传图片</td>
              <td style="text-align:center">
                 <img onerror="this.src='<%=path%>/img/noimage.jpg'" width="90" height="60" alt="" src="<%=path%>/img/noimage.jpg"/>
              </td>
              </tr> 
              <tr>
  <td class="field">联系方式:</td>
  <td>
     <s:textfield cssClass="text" key="contact" name="house.contact" />
     </td>
     </tr>

<tr>
    <td class="field">详细信息</td>
    <td><s:textarea name="description">
    </s:textarea>
    </td>
</tr>
    </table>
    <div class="buttons"><input type="button" id="but" name="button" value="立即发布" /></div>
<!-- 结束看看有没有 -->
</div>
</s:form>   
</div>
</div>
</div>    
 </body>
</html>

------解决思路----------------------
你这是struts2的s;form并不是html的form ; 
所以这个documnet.getElementByID("but").submit();  这个方法submit();这是不存在是   会出现一个is function 的error; 
 看懂了吗;
------解决思路----------------------
可以查看struts2 的form生成的html表单id,在调用...
  相关解决方案