English 中文(简体)
Java脚石
原标题:Javascript behaving weirdly

我认为,以下职能不可行。 出于某种原因,只要验证功能的一方不实,便会发生假。 为什么如此?

我的法典如下:

function validateGroup(input) {
    if (!input.value.match(/^[0-9]{0,2}$/)) {
        $(input).addClass("invalidField");
        return false;
    }
    $(input).removeClass("invalidField");
    return true;
}

function validateClass(input) {
    if (!input.value.match(/^[a-zA-Z0-9-]{0,9}$/)) {
        $(input).addClass("invalidField");
        return false;
    }
    $(input).removeClass("invalidField");
    return true;    
}

function validateData() {
    var rows = document.getElementsByTagName("tbody")[0].getElementsByTagName("tr");

    var valid = true;

    for (var i = 0, arrayLength = rows.length; i < arrayLength; ++i) {
        valid = valid && validateClass(rows[i].getElementsByTagName("input")[0]);
        valid = valid && validateGroup(rows[i].getElementsByTagName("input")[1]);
        valid = valid && validateGroup(rows[i].getElementsByTagName("input")[2]);     
    }
    return valid;
}

感谢很多!

最佳回答

有效声明和计划; 验证Class(......)将不称有效。 如果有效,则采用分类方法。 我认为,你想要做的是改变这些人的次序。

valid = validateClass(rows[i].getElementsByTagName("input")[0]) && valid;
valid = validateGroup(rows[i].getElementsByTagName("input")[1]) && valid;
valid = validateGroup(rows[i].getElementsByTagName("input")[2]) && valid;

Javascript don t bother Assessing the Others of an && expression if it have knowns that the result is false.

问题回答

即便“有效”已经被定为虚假,但你也希望对每一迭代行使有效职能。 然而,“和”以及“你正在使用”的操作将短路,因此,尽管该通道将继续发挥验证功能,但不会要求随后的频率。

真正简单的替代办法是:

for (var i = 0, arrayLength = rows.length; i < arrayLength; ++i) {
   if(!validateClass(rows[i].getElementsByTagName("input")[0]))  valid = false;
   if(!validateGroup(rows[i].getElementsByTagName("input")[1]))  valid = false;
   if(!vvalidateGroup(rows[i].getElementsByTagName("input")[2])) valid = false;
}

如同这一职能的意图一样。 三行

valid = valid && validate...

也就是说,如果任何验证功能都打上了伪造的<代码>valid<>/code>,则该休息时间仍为假。

我认为,这是因为lazy evaluation。 Java计划使用<代码>&&。 单列<代码>&。

短路评估: 共同方案语言支助

它称为短路。 快速反应:将每一行改为

valid = validateClass(rows[i].getElementsByTagName("input")[0]) && valid;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签