`
2277259257
  • 浏览: 499109 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Ajax提交表单

阅读更多

  ajax提交form表单 
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>send string</title>
    <script language=javascript> 
        var xmlhttp;
        function createxmlhttprequest(){
              if(window.ActiveXObject) {
                  xmlhttp=new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ?

                                                             'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
              }
       }

      function sendstring(){
            for(var i=0;i<2;i++){
                createxmlhttprequest();
                xmlhttp.onreadystatechange=statechange;
                xmlhttp.open('POST','ajax.aspx',false);
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(getFormQueryString("ff"));
           }
     }

      function statechange(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("x").innerHTML=xmlhttp.responseText;
            }
       }

      function Button1_onclick() {
           sendstring();
      }
//得到一个表单里的全部信息
      function getFormQueryString(frmID){
           var frmID=document.getElementById(frmID);
           var i,queryString = "", and = "";

           var item; // for each form's object

           var itemValue;// store each form object's value

           for( i=0;i<frmID.length;i++ ) {

               item = frmID[i];// get form's each object

               if ( item.name!='' ) {

                     if ( item.type == 'select-one' ) {

                            itemValue = item.options[item.selectedIndex].value;

                     }else if ( item.type=='checkbox' || item.type=='radio') {

                            if ( item.checked == false ){

                                   continue;   

                            }

                           itemValue = item.value;

                     }else if ( item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image'){    // ignore this type

                            continue;

                     }else {

                            itemValue = item.value;

                     }

                     itemValue = encodeURIComponent(itemValue);

                     queryString += and + item.name + '=' + itemValue;

                     and="&";

              }

         }

             

           return queryString;

      }

   </script>
</head>
<body  >
   <form id=ff name=ff>
      <input  type=text id="tbx" name="tbx" />
      <input id="Button1" type="button" value="button" language="javascript"

                                                                                    onclick="return Button1_onclick()" /><br />
      <br />
      <br />
      <div id=x></div>

      asdf<input id="Checkbox1" name="weew" type="checkbox" />
             <input id="Radio1" name="sdfsdf" type="radio" />
             <input id="Radio2" checked="checked" name="fff" type="radio" />
             <textarea id="TextArea1" cols="20" name="wr2234" rows="2"></textarea>
             <select id="Select1" name="fff">
                <option selected="selected" value="1">dsf</option>
                <option value="2">214312</option>
                <option value="3">23421</option>
            </select>
     </form>
</body>

</html>

 

Ajax如何无刷新提交form里面的内容,而不是一个值一个值的提交

 

     function httpPostForm(frmID, URL, data, contentType) {

             //alert(URL);

              URL += ('?' + getFormQueryString(frmID));

              //alert(URL); 

              this.init(URL,data,contentType);  

              return trim(this.httpObj.responseText);  

       }

 

      function getFormQueryString( frmID ){

           var i,queryString = "", and = "";

           var item; // for each form's object

           var itemValue;// store each form object's value

           for( i=0;i<frmID.length;i++ ){

               item = frmID[i];// get form's each object

               if ( item.name!='' ){

                      if ( item.type == 'select-one' ){

                             itemValue = item.options[item.selectedIndex].value;

                      }else if ( item.type=='checkbox' || item.type=='radio'){

                             itemValue = item.value;

                      }else if ( item.type == 'button' || item.type == 'submit' ||tem.type == 'reset' ||

                                                                                                        item.type == 'image'){

                             continue;

                      }else{

                             itemValue = item.value;

                      }

                      itemValue = encodeURIComponent(itemValue);

                      queryString += and + item.name + '=' + itemValue;

                  }

               if ( item.checked == false ){

                   continue;   

                   and="&";

               }

        }     

        return queryString;
      }

 

 

JSP

   function doCheckSubmit( actionUrl ){

        var ajaxObj = new AjaxObject();

        //actionUrl = actionUrl + "?userName=" + document.loginFrm.userName.value;

        var res = ajaxObj.httpPostForm(loginFrm,actionUrl);

   

        document.getElementById('checkResultDiv').innerHTML = '<B>' + res + '</B>';

   }

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics