Tag: 每个

使用jQuery求和每个没有全局变量的函数

我想添加一些具有相同类名的HTML元素。 所以,代码将与jQuery一样。 $(“.force”).each(function (){ a += parseInt( $(this).html()); }); $(“#total_forces”).html(a); 在此代码中,变量必须是全局变量。 有没有其他优雅的方法来总结每个.force值并从each函数中得到总和而没有全局变量?

如何在jQuery中编写一个循环,等待每个函数在继续循环之前完成

如果这是显而易见的,请原谅我。 我在页面上有一个未知数量的元素,我需要一次循环一个并执行操作。 但是,我需要循环暂停,直到元素上使用的函数完成,然后继续下一次迭代。 我尝试在$ .each循环中执行此操作,但它快速启动命令并完成而无需等待它们完成。 有任何想法吗? $(‘elem’).each(function(){ $(this).fadeIn().wait(5000).fadeOut(); }); 这就是我所拥有的,非常简单。 我从这里得到了wait()函数: jquery cookbook site 。 问题是,循环不等待 – 实际命令按预期工作,只是它们一次全部消失。 任何帮助表示感谢,谢谢。 编辑:执行此操作后,我可能希望再次执行循环,以便elems列表将按顺序再次淡入/淡出 EDIT2:从那以后得到了1.4.2 jQuery lib,使用的是1.3.2,因此自定义了wait()函数。 现在使用lobstrosity提到的delay()。 管理从他的答案拼凑一些接近我需要的东西,谢谢lobstrosity :)。

等待每个jQuery

我试图让每个声明中的div淡入/淡出。 问题是在淡入/淡出完成之前调用下一个项目。 one two three $.each([ “one”, “two”, “three”], function() { console.log( ‘start – ‘ + this ); animate( this ); console.log( ‘end – ‘ + this ); }); function animate( id ) { box = ‘#’ + id; $(box).fadeOut( 500, function( ) { console.log(‘showing – ‘ + id); $(box).fadeIn( 500 ); $(box).css(‘backgroundColor’,’white’); }); } […]

使用Jquery each()循环通过输入字段进行validation

我正在创建一个表单,并且只有在输入值是数字时才希望代码执行。 我试图避免使用某种validation插件,并想知道是否有办法循环输入字段并检查值。 我一直在尝试以下但我认为我的逻辑错了: (#monthlyincome是表单ID) $(“#submit”).click(function() { $(“#monthlyincome input”).each(function() { if (!isNaN(this.value)) { // process stuff here } }); }); 有任何想法吗? 这是整个更新的代码: $(“#submit”).click(function() { $(“#monthlyincome input[type=text]”).each(function() { if (!isNaN(this.value)) { // processing data var age = parseInt($(“#age”).val()); var startingage = parseInt($(“#startingage”).val()); if (startingage – age > 0) { $(“#field1”).val(startingage – age); $(“#field3”).val($(“#field1”).val()); var inflationyrs = parseInt($(“#field3”).val()); […]

每个人等到完成$ .ajax,然后继续

function genTask(elem){ elem.each(function(){ $this=$(this).parent(‘.cntTasks’); var pattern=/taskId-(.*)$/ var idTask=$this.attr(‘id’).match(pattern); var data=’id_task=’+idTask[1]; if(typeof jsVar2 !=’undefined’) data+=jsVar2; $.ajax({ type: “POST”, url: domain+”/view_tasks/gen_tasks/”, dataType: ‘html’, data: data, success: function(dt){ $this.find(‘.contChildTasks’).html(dt); childs=$this.children(‘.taskDesc’).find(‘.has_child’); if(childs.length!=0) genTask(childs); } } }); $this.find(‘.taskDesc’).show(); }); } if(typeof jsVar2 !=’undefined’) genTask($(‘.cntTasks .has_child’)); }); 怎么可能让$.each等到动作$.ajax完成,然后继续循环,我不能得到$ this var,因为它有最后一个值,对不起我的英语,谢谢!

迭代jquery中的嵌套表单元素

我很抱歉,如果这已经发布我一直在寻找无济于事.. 我只是想知道如何在jquery中循环嵌套表单’elements’(元素不仅是输入标签之类的严格表单元素,还包括其他html元素)。 目前我有这段代码来做: $(‘#’+arguments[i].formid).children().each(function(){ var child = $(this); alert(child.attr(‘id’)); if(child.is(“:input”)) { alert(child.attr(‘id’)); if(child.attr(‘id’)!=”) eval(“p.”+child.attr(‘id’)+”='”+child.attr(‘value’)+”‘”); } if(child.is(“:textarea”)) { if(child.attr(‘id’)!=”) eval(“p.”+child.attr(‘id’)+”='”+child.attr(‘value’)+”‘”); } }); 当我的表单包含这样的其他元素时,它不起作用: … 请帮忙…

如何检查jquery中的空attr()?

我有一些使用PHP创建的div。 div中的锚点总是有一个HREF,即使它是空白的。 基本上,我试图检测HREF是否为空白。 如果它有内容,则不执行任何操作,如果它是空白的,则删除文本,删除锚点,然后将文本放回原位。 这是div: Lorem Ipsum 这是我的代码: jQuery(document).ready(function($) { //required for $ to work in WordPress $(“.article”).each(function(){ if ($(this).attr(‘href’) !== undefined) { return; } else { var linkTitle = $(this).html(); $(this).parent().empty().html(linkTitle); } }); //–> });

使用jQuery通过.each()获取数据属性值

我有以下带有数据属性的HTML – 我想编写一些jQuery,它将循环遍历HTML并收集数据属性并将它们放入数组中 – 任何人都可以协助我收到错误。 ERROR in console log : item.data is not a function 我正在尝试使用data()属性 – 你能看到我做错了吗? //我的HTML代码 //我的jQuery代码 var multi = $(‘.winners’); var winners_array = []; $.each(multi, function (index, item) { winners_array.push( {name: ‘fullname’, value: item.data(‘fullname’)} ); }); console.log(winners_array); // ERROR in console log : item.data is not a function

jQuery Ajax / .each回调,在ajax完成之前下一个’each’触发

嗨,当我提交表单时,会调用以下Javascript。 它首先从文本区域中分割出一堆url,然后: 1)为每个url添加一个表格,在最后一列(“状态”列)中显示“未开始”。 2)再次循环遍历每个URL,首先关闭它进行ajax调用以检查状态(status.php),该状态将返回0-100的百分比。 3)在同一循环中,它通过ajax(process.php)启动实际进程,当进程完成时(记住连续状态更新),它将在状态列中显示“已完成”并退出自动刷新。 4)然后它应该转到下一个’each’并为下一个url做同样的事情。 function formSubmit(){ var lines = $(‘#urls’).val().split(‘\n’); $.each(lines, function(key, value) { $(‘#dlTable tr:last’).after(”+value+’Not Started’); }); $.each(lines, function(key, value) { var auto_refresh = setInterval( function () { $.ajax({ url: ‘status.php’, success: function(data) { $(‘#dlTable’).find(“tr”).eq(key+1).children().last().replaceWith(“”+data+””); } }); }, 1000); $.ajax({ url: ‘process.php?id=’+value, success: function(msg) { clearInterval(auto_refresh); $(‘#dlTable’).find(“tr”).eq(key+1).children().last().replaceWith(“completed rip”); } }); }); }

带有输入元素的jQuery .each()

//save tablet jQuery(“#savetablet”+jTablets[i].idtablets).on(‘click’, function() { alert(“alertsepy2…”); console.log(jTablets[i].idtablets); jQuery(“#tablet”+jTablets[i].idtablets+” .detailsrow”).each(function( index ) { $(this).each(function( index2 ) { console.log($(this).html()); }); }); }); 11 21 我有上面的jQuery .each(),它将附加的HTML输出到控制台。 在这种情况下,我想只提取类型为text或type number的输入元素的val()。 有没有办法隔离输入元素,以便我可以将它们的值输出到数组中? 提前致谢…