如何使用jQuery来识别一个;更改“;具有许多动态表单元素的事件
我有一个web表单,它是用几十个/数百个表单元素文本字段动态构建的 有没有办法使用如何使用jQuery来识别一个;更改“;具有许多动态表单元素的事件,jquery,onchange,Jquery,Onchange,我有一个web表单,它是用几十个/数百个表单元素文本字段动态构建的 有没有办法使用$().change(function(){})或其他一些方法来(a)调用jQuery/Ajax在后台执行某些操作,以及(b)捕获实际更改的特定表单元素并使用其更改的值 我可以处理Ajax部分。我在onChange事件中失败了 谢谢 *JS文件看起来像这样* jQuery.support.cors=true;//ajax需要在某些较旧的浏览器和版本中工作 $(文档).ready(函数(){ $('a[rel^=“p
$().change(function(){})代码>或其他一些方法来(a)调用jQuery/Ajax在后台执行某些操作,以及(b)捕获实际更改的特定表单元素并使用其更改的值
我可以处理Ajax部分。我在onChange事件中失败了
谢谢
*JS文件看起来像这样*
jQuery.support.cors=true;//ajax需要在某些较旧的浏览器和版本中工作
$(文档).ready(函数(){
$('a[rel^=“prettypto”]”)。prettypto({
changepicturecallback:函数(){
//警惕(“保持专注”);
}
});
//查找表单中的所有表单元素,
//然后将事件处理程序绑定到“change”事件的所有元素
$(“#我的表单”).find('input,textarea').on('change',function(event){
//这现在指的是“已更改”元素
var name=this.name,
value=this.value,
id=this.id,
dObj={};//创建要作为数据参数传递给AJAX请求的对象
//将键设置为输入的名称,将值设置为输入的值
dObj[name]=值;
$.ajax({
url:“”,
data:dObj,//传递数据对象
//成功:函数(数据){…},
成功:警报(“更改的CCC”),
错误:函数(jqXHR、textStatus、errorshown){/*不允许get处理错误*/}
});
});
$(“#我的表单”)。在('change','input,textarea',函数(event)上{
警报(“变更AAA”);
});
$(文档).on('change','my form input,#my form textarea',函数(事件){
警报(“变更BBB”);
});
}); // 完
//window.parent.closePP();
这假设在绑定时,#我的表单
在DOM中,否则您可以绑定到文档
:
$(document).on('change', '#my-form input, #my-form textarea', function (event) {
...
});
谢谢你的帮助@Jasper。我尝试了你的答案的所有3个版本,但都没有成功。(请参阅我刚才附加在问题后面的代码)。请注意,我已将.JS文件放在结束标记的前面。@Dr.DOT您的表单是否具有我的表单的ID
?因为我的JS代码正在尝试选择ID为my form
的表单中的input
元素。如果表单没有该ID,请更改选择器以查找正确的表单,或者为表单提供my form
ID。
//find all the form elements in your form,
//then bind an event handler to all of the elements for the `change` event
$('#my-form').find('input, textarea').on('change', function (event) {
//this now refers to the "changed" element
var name = this.name,
value = this.value,
id = this.id,
dObj = {};//create object to pass as data parameter to AJAX request
//set the key as the name of the input, and the value as the value of the input
dObj[name] = value;
$.ajax({
url : '<url>',
data : dObj,//pass the data object
success : function (data) { ... },
error : function (jqXHR, textStatus, errorThrown) { /*don't for get to deal with errors*/ }
});
});
$('#my-form').on('change', 'input, textarea', function (event) {
...
});
$(document).on('change', '#my-form input, #my-form textarea', function (event) {
...
});