0

我不明白为什么这个简单的功能不能按我想要的方式工作:)

c = document.form.product_catid.selectedIndex;
if (document.form.product_name.value == "" && document.form.product_catid[c].value == "")
{
    alert ("Please, define at least product name or product category !");
    return false;
}
else if (!(document.form.product_name.value == "")) 
{
    return true;
}
else if (!(document.form.product_catid[c].value == "")) 
{
    return true;
}
else
{ 
    alert ("Please, dont define two of them at the same time!");
    return false; 
}
return true;

我想要的只是当输入名称为product_name 或选择名称为product_catid 时,该函数返回true,但如果没有定义它们或两者都定义,我希望它发出两个不同的警报)谢谢大家的帮助, 对此,我真的非常感激!

4

4 回答 4

2

尝试使用 Chrome 开发者工具、Firebug 或类似工具来调试您的案例。我的怀疑是你试图在子句中比较""and (当is 时就是这种情况)。试试吧。undefineddocument.form.product_catid[c].value == ""selectedIndex-1!document.form.product_catid[c].value

于 2012-05-29T13:59:11.043 回答
1

使用return语句后,函数“结束”:

alert ("Please, define at least product name or product category !");
return false; // Exit point
...
...
// this will not be executed.
alert ("Please, dont define two of them at the same time!"); 
return false;

var returnValue = true;

if (document.form.product_name.value == "" && document.form.product_catid[c].value == "")
{
    alert ("Please, define at least product name or product category !");
    returnValue = false;
}
else if (document.form.product_name.value == "") 
{
    return true;
}
else if (document.form.product_catid[c].value == "") 
{
    return true;
}
else
{ 
    alert ("Please, dont define two of them at the same time!");
    returnValue = false; 
}
return returnValue;​
于 2012-05-29T13:46:28.547 回答
0

试试这个:

var product = document.form.product_name.value;
var category = document.form.product_catid[document.form.product_catid.selectedIndex].value
if (product || category) {
    if (product && category) {
        alert ("Please, dont define two of them at the same time!");
        return false;
    }
    else return true;
}
else {
    alert ("Please, define at least product name or product category !");
    return false;
}
于 2012-05-29T14:02:08.067 回答
0

您的条件排序不正确。

就目前而言:

  1. 您的第一个检查是两者都处理两个输入均为空的情况,这按预期工作。
  2. 您的下一个检查是其中一个值不为空
  3. 下一个检查是另一个值不为空
  4. 那么您假设此时两个输入都不为空(您的else条件)

考虑一下:条件 1 确保两个输入都不为空 - 这意味着,从逻辑上讲,其中至少一个是 non-empty。在此之后,鉴于我们现在知道其中至少有一个是非空的,条件 2 或条件 3将始终计算为true。这样做的结果是第四条代码路径永远不会执行。

要解决此问题,请检查两个输入是否未填充为显式条件。:

function validateInput() {

    //replace values here to test function
    var productCatId = "",
        productName = "gjghj";

    //uncomment when using real values
    //var productCatId = document.form.product_catid[document.form.product_catid.selectedIndex].value,
    //    productName = document.form.product_name.value;

    //check both aren't empty
    if (productName == "" && productCatId == "")
    {
        alert ("Please, define at least product name or product category !");
        return false;
    }
    //check both aren't populated
    else if (productName != "" && productCatId != "")
    { 
        alert ("Please, dont define two of them at the same time!");
        return false; 
    }
    //by now we know that both aren't empty, and both aren't populated, therefore only one is populated, so return true;
    return true;

}
于 2012-05-29T14:19:43.757 回答