js获取div中的文本框数据

时间:2024-03-04 14:44:10

通过div得到div里的所有数据

大神的世界无需解释,当然不是说我,当我看到这些代码的时候我惊呆了!

 

 

这是一个工具方法js:

js:

 /*
     * 获取指定对象下的所有input、textarea值
     * divid:外层的id
     * jsonStr:如果无,新创建一个对象,如果有,直接在对象中添加
     * isCheck:是否进行必填项判断
     */
    getFormParam: function(divid,jsonStr,isCheck){
        var inputlist = $("#"+divid+" input[type=text]");
        var textarealist = $("#"+divid+" textarea");
        var param = {};
        if(jsonStr){
            param = jsonStr;
        }
        
        var iserror =false;
        $.each(inputlist,function(i,obj){
          var id = obj.id;
          var name = obj.name;
          if(!id && !name){
              
          }else{
              var ligeruiid = obj.attributes.ligeruiid;
              var val = $.trim(obj.value);
              if(val == \'\'){
                  if(isCheck && $(obj).attr("isrequired") == \'1\'){
                      if(obj.style.display == \'none\' || (obj.parentNode && obj.parentNode.style.display == \'none\')){//父节点是否隐藏
                          
                      }else{
                          iserror =true;
                          obj.style.border = \'1px dotted #FF0000\';
                          //$(obj).attr({"style":"border: 1px dotted #FF0000;"});
                      }
                  }
              }else{
                  obj.style.border = \'\';
              }
              if(ligeruiid){
                  val = $("#"+id+"_hidden").val();
              }
              if(name){
                  param[name] = val;
              }else{
                  param[id] = val;
              }
          }
        });
        $.each(textarealist,function(i,obj){
          var id = obj.id;
          var name = obj.name;
          if(!id && !name){
              
          }else{
              var val = $.trim(obj.value);
              if(val == \'\'){
                  if(isCheck && $(obj).attr("isrequired") == \'1\'){
                      if(obj.parentNode && obj.parentNode.style.display == \'none\'){//父节点是否隐藏
                          
                      }else{
                          iserror =true;
                          obj.style.border = \'1px dotted #FF0000\';
                          //$(obj).attr({"style":"border: 1px dotted #FF0000;"});
                      }
                  }
              }else{
                  obj.style.border = \'\';
              }
              if(name){
                  param[name] = val;
              }else{
                  param[id] = val;
              }
          }
        });
        if(iserror){
            return \'error\';
        }
        return param;
    }

 

 

    
    html:
    
    <div>
        <div class="l-panel-search-cond clearfix" id=\'main_t\'>
                    <div class="float-l">
                        <div class="l-panel-search-title">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;借款用途:</div>
                        <div class="l-panel-search-item">
                            <input type="text" id="credit_purpose" style="width:430px" isRequired="1"/>
                        </div>
                    </div>
                    <div class="float-l ">
                        <div class="l-panel-search-title">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;信贷产品种类:</div>
                        <div class="l-panel-search-item">
                            <input type="text" id="cre_loan_type" ligeruiSelectIsRequired="1"/>
                        </div>
                    </div>
                    
                    <div class="float-l clearboth">
                        <div class="float-l">
                            <div class="l-panel-search-title">申请贷款额度:</div>
                            <div class="l-panel-search-item">
                                <input type="text" id="credit_limit" style="width:95px" isRequired="1" isFloat="1" minVal="0" maxVal="100000000"/> (万元)
                            </div>
                          </div>
                          <div class="float-l">
                        <div class="l-panel-search-title">可接受每月最高还款额:</div>
                        <div class="l-panel-search-item">
                            <input type="text" id="max_repayment_limit_per_month" style="width:95px" isRequired="1" isFloat="1" minVal="0" maxVal="1000000"/> (元/月)
                        </div>
                        </div>
                    </div>
                    <div class="float-l">
                        <div class="l-panel-search-title">&nbsp;申请最长还款期限:</div>
                        <div class="l-panel-search-item">
                            <input type="text" id="max_repayment_time_limit" style="width:95px" isRequired="1" isPositiveInteger="1" minVal="1" maxVal="120" scope="a"/> (月)
                        </div>
                    </div>
                    <div class="float-l">
                        <div class="l-panel-search-title">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共贷情况:</div>
                        <div class="l-panel-search-item">
                            <input type="text" id="hasconmpre" style="width:95px" isRequired="1" />
                        </div>
                    </div>
                </div>
    
    </div>

 

 

将div里的值获取后传向后台:

var jsonStr;

jsonStr = globalUtil.getFormParam(\'main_t\',null,false);
        $("#tb_btn").css("display","none");//隐藏按钮 
        $.post(globalUtil.getTimestampUrl("/cremanage/mcscrecreditheadupdate.do"),
                jsonStr, function(data) {
                    if (data === \'OK\') {
                        globalUtil.successMsg(globalErrorMsg[\'100002\'],
                                function() {
                                    //关闭TAB 刷新列表
                                    closeTabAndRes();
                                });//保存成功
                    }else {
                        globalUtil.errorMsg(globalErrorMsg[\'100012\']); //保存失败
                        $("#tb_btn").css("display","");//显示按钮 
                    }
                });