jQuery的提交功能有时工作 [英] JQuery submit function working sometimes

查看:83
本文介绍了jQuery的提交功能有时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个包含一个使用jquery的jsp文件的类制作Web应用程序.我希望该应用在提交之前立即触发警报.这在我的真实程序中有时会起作用,而在其他时候则永远不会触发警报.我似乎无法确定一个实例,该实例始终有效或永远无效.除非添加Firebug断点,否则我的错误控制台对此问题保持沉默.然后它给了我

I'm making a web application for a class which contains a jsp file that uses jquery. I want the app to trigger an alert right before submitting. This works some of the time in my real program, and other times the alert is never triggered. I can't seem to peg down an instance where it always works or never works. My error console is silent on the matter, unless I add Firebug breakpoints. Then it gives me

Error: attempt to run compile-and-go script on a cleared scope
Source File: http://code.jquery.com/jquery-1.7.2.min.js
Line: 2

但是我不知道那是什么意思.就Firebug而言,我不明白为什么警报消息之前停止评估.

But I have no idea what that means. As far as Firebug goes, I can't understand why the evaulation stops before the alert message.

我试图对问题进行记录,但是我想它与我的真实程序有很大不同,因为提交从未起作用.我将显示真实程序中的一些代码. (对不起缺少SSCCE.)

I tried to make an sscce documenting the problem, but I guess it was too different from my real program, because submit never worked. I'll show some code from my real program. (Sorry about the lack of SSCCE.)

$(document).ready(function() {
    $("#questionDisplay").submit(function() {
        var correctAnswer = $(".correctAnswer").attr("value");
        var answer = "";

        if ($(".multipleChoice").length > 0) {
            answers = $("input:checked");
            for (var obj in answers) {
                answer += obj.attr("value") + "#";
            }
        } else if ($(".fillBlank").length > 0) {
            for (var answerNo = 1; $(".answer" + answerNo).length > 0; ++answerNo) {
                answer += $(".answer" + answerNo).attr("value") + "#";
            }
        } else {
            answer = $(".answer1").attr("value");
        }

        if (answer == correctAnswer) {
            alert("Yes! Correct!");
        } else {
            alert("Sorry, incorrect.");
        }
    });
});

jsp是一团糟(我们必须使用scriplets :(),但是如果您只想看一下,那就知道了.

The jsp is a huge mess (we have to use scriplets :( ), but if you'd like to see it just lemme know.

如何让我的提交处理程序每​​次工作?

How do I get my submit handler to work every time?

推荐答案

在您的示例中,answers是元素的jQuery集合.使用for(var obj in answers) { }遍历它实际上是遍历答案的属性,而不是元素本身.因此,在属性上调用.attr()是行不通的.

In your example, answers is a jQuery collection of elements. Looping through it using for(var obj in answers) { } is actually looping through the properties of answers, not the elements themselves. Therefore, calling .attr() on a property is not going to work.

通常,如果我看到调试器状态jQuery有错误,那么我在非jquery选定对象上调用jQuery方法的时间是99.9%.在这种情况下,我看到了Chrome的JavaScript控制台中的错误,有时结果可能因不同的控制台而异.

In general, if I see my debugger state that jQuery has an error, it's 99.9% of the time me calling a jQuery method on a non-jquery selected object. In this case, I saw the error in Chrome's JavaScript console, and sometimes results may vary with different consoles.

一个好的做法是在存储jQuery元素的变量前加$表示它们是jQuery对象.例如,$answers使跟踪其内容变得更加容易.

A good practice is to prefix variables that store jQuery elements with $ to indicate that they are jQuery objects. For instance, $answers makes it easier to keep track of what it contains.

使用:

answers.each(function() {
     answer += $(this).attr("value") + "#";
});

代替此:

for (var obj in answers) {
    answer += obj.attr("value") + "#";
}

这篇关于jQuery的提交功能有时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆