根据组合框中的选定值自动填充表单,使用 Jquery 和 Ajax
Autopopulate form based on selected value from combo box with Jquery and Ajax
我是ajax和Jquery的新手。我已经编写了代码,以我正在设计的形式中的 ajax 调用自动填充组合框。现在我想根据组合框中的选定值自动替换相同形式的其余字段,为此我需要另一个 ajax 调用,它将只返回一个student
对象。有了这个对象,我可以像这样设置表单的字段:
$('#student_name').val(student.name);
$('#student_roll_num').val(student.roll_num); etc.
其中,名称、roll_num是表单字段的 ID。
我以以下方式写:
//This following function will get called when one value from the combo box is
//selected
$("select#student_id").change(function(){
var student_id = $(this).val(); //Here I am getting the selected value from
//the combo box
$.ajax({
url: "/students_database?student_id="+student_id, //this is the url that will
//send me one student object
dataType: "json",
success: /*YET TO BE FILLED WITH CODE TO AUTO
/* POPULATE REST OF THE FIELDS*/
});
实际上,在这里我无法决定如何访问重新调整的学生对象,以便我可以像上面所说的那样访问其字段。所以,如果有人好心地帮助我做到这一点,我将不胜感激。另外,如果有更好的方法,请提出建议。谢谢。
假设你从 ajax 调用中获取了一个学生的 json,这应该可以工作:
$("select#student_id").change(function(){
var student_id = $(this).val(); //Here I am getting the selected value from
//the combo box
$.ajax({
url: "/students_database?student_id="+student_id, //this is the url that will
//send me one student object
dataType: "json",
success: function(student) {
$('#student_name').val(student.name);
$('#student_roll_num').val(student.roll_num);
}
});
});
相关文章:
- 单击鼠标,用MySQL数据填充html表单输入字段
- 通过浏览器对表单自动填充做出反应
- 谷歌表单-根据分配给谷歌应用程序的工作为商业用户预先填充多项选择列表
- 提交表单时,Javascript(JQuery)不会在文本区域中填充内容
- Javascript未在开发服务器上填充Perl表单
- 如何在js中实现树结构类型的表单动态填充
- CasperJS填充表单-输入名称有方括号
- 如何使用多维JSON数据重新填充表单
- 试图用cookie中的信息填充表单
- 如何在刷新时重新填充表单中的复选框并将表单值发布到页面
- 单击一个数据表,填充第二个数据表
- 将表单填充到要与ngMessages一起使用的templateURL中
- jQuery表单填充器不工作
- Rails 输入表单填充了 jQuery .val(),但参数中不存在数据
- 使用自动填充表单控件(自动完成属性),加快表单填充速度
- HTML5地理位置地址表单填充器未捕获语法错误:意外的标记<
- 使用AJAX调用进行表单填充和验证
- 在检索数据时,表单填充然后清除
- 基于浏览器的客户端表单填充
- 从Google表单填充HTML表单