JS动态生成Form并用Ajax方式提交的实现方法

作者:半夏

时间:2026-10-30

浏览:389

本文介绍在JavaScript中动态创建form元素并通过Ajax方式提交数据的方法。主要涉及使用Ext JS框架,通过document.createElement动态生成form和input元素,设置其属性和值,最后利用Ext.Ajax.request进行异步提交。该方法适用于需要动态构建表单并提交数据的场景。

文章目录

JavaScript动态生成Form并通过Ajax提交数据的方法

以下是相关的JavaScript代码示例:


var tempForm = document.createElement("form");   
 tempForm.action="http://localhost:8080/test/user";   
 tempForm.method="post";   
 document.body.appendChild(tempForm);   
    
    //create a submit button    
 var tempInput = document.createElement("input");   
 tempInput.type="hidden";   
 tempInput.name="method";    
 tempInput.value=methodName; //the parameter of method in the code of DispatchAction.   
   
 tempForm.appendChild(tempInput);   
    //submit the form   
 tempForm.submit();  
 
 Ext.Ajax.request({  
    form:'intPayForm',  
    method:'post',  
    success:function(data){  
      alert(data.responseText);  
    },  
    failure:function(){  
    }  
  }); 

该写法在Ext 3版本中得到了支持,其他版本尚未进行测试验证。

以上就是JavaScript动态生成Form并通过Ajax方式提交的实现方法的全部内容,希望能为读者提供参考,并欢迎大家继续支持本站。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章