javascript - 如何使用 AJAX 生成表单?

标签 javascript html ajax forms

如何生成 ajax 表单?

你保留<form>吗? - javascript 文件中的代码并使用 ajax(例如 JSON)加载数据,或者加载生成的 html 文件(包含您刚刚推送到浏览器的所有 <form><input>)?或者还有其他更简单的方法吗?

如果您使用任何框架,您是否可以从模型中自动生成表单?

您认为什么最容易保持和维护? 有没有您觉得有用的插件或库,也许是 jQuery 的一些东西?

最佳答案

jQuery库中有一个解决方案

 jQuery.get( url, [data], [callback], [type] ) 

Load a remote page using an HTTP GET request. This is an easy way to send a simple GET request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). If you need to have both error and success callbacks, you may want to use $.ajax.

$.get() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually.

看看 jQuery Documentation

示例:

$.get("someForm.pl", { name: "John" },
  function(data){
    $(data).appendTo(document.body); // you might place it somewhere else
  });

编辑:

仅更改现有 dom 的值的示例:

<form id="myForm"><input id="myName" /></form>

$.get("someForm.pl", { name: "John" },
  function(data){
    $("#myForm").each(function(){
      this.value = data[this.id];
    });
  },"json");

您的服务器响应将:

{ 'myName' : 'John' }

关于javascript - 如何使用 AJAX 生成表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1538579/

相关文章:

javascript - 如何使用javascript从gridview中过滤任何项目

javascript - 在表格内创建 Highcharts 饼图图例

jquery - HTML5音频文件播放或暂停时向div添加一个类

javascript - 使用 JavaScript 按公共(public)值对对象属性进行分组

android - 移动视口(viewport)尺寸是否大于屏幕尺寸?

javascript - 为什么这个 switch 语句不起作用?

javascript - 如何在不同范围内更新模型数组中的项目,并使更改反射(reflect)到各处?

javascript - 使用 AJAX 和 Jquery 从服务器请求数据

php - Ajax、PHP/MySQL 搜索请求不断崩溃

javascript - AJAX - 每当调用 AJAX 脚本时,Javascript 就会中断