尝试让一些 jQuery 函数按顺序运行。回调是问题吗?
我试图按顺序做一些事情,但遇到了一些麻烦。
- 当单击 id #sub_button 的按钮时,
- 请确保每个具有类“.verify”的元素都有其自己的对象值(参见代码)...
- ...如果没有,则模糊该元素(将运行一些其他代码并创建一个对象)。
上述 IF 检查完成后(现在所有元素都应该有一个对象),然后运行函数“isitgood”。 (“isitgood”函数在所有元素获取其对象值之前运行,这是在模糊时完成的)
$("#sub_button").click(function() { $(".verify").each(function(){ objtitle = $(this).attr('id'); if (!myObj[objtitle]) { $("#"+objtitle).blur(); // 定义任何未定义的内容 } }); // 结束每个 很好(); }); // 结束点击函数 函数 isitgood(){ if (myObj.login_id == "ok" && myObj.email == "ok") { // 提交表单 } 别的 { // 显示错误 } }
此外,一旦我以正确的顺序执行该函数,最好执行某种 .each 循环来检查是否所有对象值==“ok”,而不是在函数中指定所有对象值。所有对象的名称(即login_id、email)都是具有类名.verify 的任何元素的ID attr。
I'm trying to do some things in order, and I'm having some trouble.
- When the button with the id #sub_button is clicked,
- Make sure each element with class ".verify" has it's own object value (see code)...
- ... if not, blur that element (will run some other code and create an object for it).
AFTER the above IF check is COMPLETE (now all elements should have an object), THEN run function "isitgood". (The "isitgood" function is running before all elements get their object values, which is done on blur)
$("#sub_button").click(function() { $(".verify").each(function(){ objtitle = $(this).attr('id'); if (!myObj[objtitle]) { $("#"+objtitle).blur(); // Define anything undefined } }); // end each isitgood(); }); // end click function function isitgood(){ if (myObj.login_id == "ok" && myObj.email == "ok") { // submit the form } else { // shows error } }
Also, once I get this executing in the right order, it would be nice to do some sort of .each loop to check if all the object values == "ok" instead of specifying all of them in the function. All of the names of the objects (ie. login_id, email) are the ID attr of any element with class name .verify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您可以在单击回调中进行快速索引检查:
这将检查您是否位于 jQuery 对象中的最后一个元素,如果是,将运行
isitgood()
函数。这样,您可以确保在执行isitgood()
之前完成$.each
方法Well, you could do a quick index check in the click callback:
This will check if you're on the last element in the jQuery object, and if so, will run the
isitgood()
function. This way, you make sure that you're finished with the$.each
method before executingisitgood()
JavaScript 是异步的。当
.each
仍然在做它的事情时,你的isitgood()
总是会触发。也就是说,从您的代码来看,尚不清楚您要实现什么目标。您使用
.each
的方式似乎表明您的标签上有多个相同的 ID 属性。这是不行的,ID 必须是唯一的。而且你似乎混合了 jQuery 和常规 Javascript。使用其中之一。实际上只要使用 jQuery,你就会节省时间和精力!如果您确实有唯一的 ID,那么您根本不需要
.each
。只需使用 if 语句检查适当的 id 即可。请提供更多您的代码,我可以用更好的答案来更新它。例如,您的
myObj
是什么样的?它的元素如何获得ok
的值?它似乎没有在您对.each()
的调用中设置。Javascript is asynchronous. Your
isitgood()
will always fire while.each
is still doing it's thing.That said from your code it's not clear what you're trying to accomplish. The way you're using
.each
seems to indicate that you have multiple of the same ID attributes on your tags. That won't work, IDs have to be unique. Also you seem to be mixing jQuery and regular Javascript. Use one or the other. Actually just use jQuery, you'll save yourself time and effort!If you do have unique ids then you shouldn't need the
.each
at all. Just check the appropriate ids with your if statement.Please provide more of your code and i can update this with a better answer. For instance what does your
myObj
look like? How do elements of it get the value ofok
? It doesn't seem to get set within your call to.each()
.