jQuery each()函数根据每个表单的id将元素追加到表单中
我试图循环浏览页面上显示的表单列表,并根据它们的id向每个表单添加元素 我曾想过这样做,但似乎不起作用:jQuery each()函数根据每个表单的id将元素追加到表单中,jquery,loops,each,Jquery,Loops,Each,我试图循环浏览页面上显示的表单列表,并根据它们的id向每个表单添加元素 我曾想过这样做,但似乎不起作用: var number_of_forms = $('.common_form_class').length; if (number_of_forms > 1) { $('.common_form_class').each(function() { var identity = $(this).attr('id'); $.getJSON("/tok
var number_of_forms = $('.common_form_class').length;
if (number_of_forms > 1) {
$('.common_form_class').each(function() {
var identity = $(this).attr('id');
$.getJSON("/token.php?key=" + identity, function(data){
$(this).append('<input type="hidden" class="token" name="token" value="'+data.token+'" />');
});
});
} else {
var identity = $('.common_form_class').attr('id');
$.getJSON("/token.php?key=" + identity, function(data){
$('.common_form_class').append('<input type="hidden" class="token" name="token" value="'+data.token+'" />');
});
}
var number_of_forms=$('.common_form_class')。长度;
如果(表格数量>1){
$('.common\u form\u class')。每个(函数(){
var identity=$(this.attr('id');
$.getJSON(“/token.php?key=“+标识,函数(数据){
$(此)。附加(“”);
});
});
}否则{
变量标识=$('.common_form_class').attr('id');
$.getJSON(“/token.php?key=“+标识,函数(数据){
$('.common_form_class')。追加('');
});
}
我知道我不能使用$(this)引用each()循环中的表单,但有人知道我如何实现这一点吗?只有当您有多个表单时才有效。如果只有一个表单试图使用
$(this)
而没有每个循环,那么this
将成为窗口
对象
另外,在回调中使用$(this)
也不起作用,因为回调是在循环范围之外的稍后执行的。将对表单的引用存储在局部变量中,以便将其包含在回调函数的闭包中
即使只有一个表单,也可以循环:
$('.common_form_class').each(function() {
var frm = $(this);
$.getJSON("/token.php?key=" + frm.attr('id'), function(data){
frm.append('<input type="hidden" class="token" name="token" value="'+data.token+'" />');
});
});
$('.common\u form\u class')。每个(函数(){
var frm=$(本);
$.getJSON(“/token.php?key=“+frm.attr('id'),函数(数据){
frm.追加(“”);
});
});
或者更好地使用jQuery:
$('.common_form_class').each(function(i, frm) {
$.getJSON("/token.php", { key: frm.attr('id') }, function(data){
frm.append($('<input/>', { type: 'hidden', 'class': 'token', name: 'token' }).val(data.token));
});
});
$('.common\u form\u class')。每个(函数(i,frm){
$.getJSON(“/token.php”,{key:frm.attr('id')},函数(数据){
追加($('',{type:'hidden',class:'token',name:'token'}).val(data.token));
});
});
注意:通过使用val
方法来设置值,而不是将其连接到HTML代码中,您不必担心可能会破坏HTML代码的字符。这只适用于具有多个表单的情况。如果只有一个表单试图使用$(this)
而没有每个循环,那么this
将成为窗口
对象
另外,在回调中使用$(this)
也不起作用,因为回调是在循环范围之外的稍后执行的。将对表单的引用存储在局部变量中,以便将其包含在回调函数的闭包中
即使只有一个表单,也可以循环:
$('.common_form_class').each(function() {
var frm = $(this);
$.getJSON("/token.php?key=" + frm.attr('id'), function(data){
frm.append('<input type="hidden" class="token" name="token" value="'+data.token+'" />');
});
});
$('.common\u form\u class')。每个(函数(){
var frm=$(本);
$.getJSON(“/token.php?key=“+frm.attr('id'),函数(数据){
frm.追加(“”);
});
});
或者更好地使用jQuery:
$('.common_form_class').each(function(i, frm) {
$.getJSON("/token.php", { key: frm.attr('id') }, function(data){
frm.append($('<input/>', { type: 'hidden', 'class': 'token', name: 'token' }).val(data.token));
});
});
$('.common\u form\u class')。每个(函数(i,frm){
$.getJSON(“/token.php”,{key:frm.attr('id')},函数(数据){
追加($('',{type:'hidden',class:'token',name:'token'}).val(data.token));
});
});
注意:通过使用val
方法来设置值,而不是将其连接到HTML代码中,您不必担心可能破坏HTML代码的字符。我使用更好的样式重新编写了代码,请测试:
$('.common_form_class').each(function() {
var $this = $(this);
$.getJSON("/token.php", {key: $this.attr('id')}, function(data){
$this.append($("<input/>", {type: "hidden", "class": "token", name: "token"}).val(data.token));
});
});
$('.common\u form\u class')。每个(函数(){
var$this=$(this);
$.getJSON(“/token.php”,{key:$this.attr('id')},函数(数据){
$this.append($(“”,{type:“hidden”,“class:“token”,name:“token”}).val(data.token));
});
});
我使用更好的样式重写了您的代码,请测试:
$('.common_form_class').each(function() {
var $this = $(this);
$.getJSON("/token.php", {key: $this.attr('id')}, function(data){
$this.append($("<input/>", {type: "hidden", "class": "token", name: "token"}).val(data.token));
});
});
$('.common\u form\u class')。每个(函数(){
var$this=$(this);
$.getJSON(“/token.php”,{key:$this.attr('id')},函数(数据){
$this.append($(“”,{type:“hidden”,“class:“token”,name:“token”}).val(data.token));
});
});
为什么您不认为可以在中使用$(this)
。每个?您可能希望在回调之前捕获它,var$form=$(this);/*其他代码*/funciton(data){$form.append(…);}
应该可以工作。好吧-我想我不能使用$(this)引用当前循环使用的元素。我知道我可以将index或key=>value对作为第一个参数(函数)的参数传递,但我不认为我可以使用$(this)来引用元素本身吗?是的,可以使用$(this)引用每个表单。你能更具体地说明什么不起作用吗?有错误信息吗?顺便说一下,如果条件没有意义。两个块都在做相同的事情。是的-抱歉,我已经更正了代码-如果页面上只找到一个表单,我将其称为:var identity=$('.common_form_class').attr('id');为什么您不认为可以在中使用$(this)
。每个?您可能希望在回调之前捕获它,var$form=$(this);/*其他代码*/funciton(data){$form.append(…);}
应该可以工作。好吧-我想我不能使用$(this)引用当前循环使用的元素。我知道我可以将index或key=>value对作为第一个参数(函数)的参数传递,但我不认为我可以使用$(this)来引用元素本身吗?是的,可以使用$(this)引用每个表单。你能更具体地说明什么不起作用吗?有错误信息吗?顺便说一下,如果条件没有意义。两个块都在做相同的事情。是的-抱歉,我已经更正了代码-如果页面上只找到一个表单,我将其称为:var identity=$('.common_form_class').attr('id');使用变量名$this
可能会令人困惑。它看起来像一个失败的$(this)
+1用于在getJSON
调用中使用数据对象。@Guffa:我知道你是从哪里来的——但是如果变量名引用jQuery对象,我更喜欢以$开头。太好了-我已经为var frm更改了var$这个-但总的来说很好。谢谢。使用变量名