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という名前のselectが選択されたときだけです。関数はtrueを返しますが、それらのいずれも定義されていない場合、または両方が定義されていない場合は、2つの異なるアラートをアラートさせたいです)助けてくれてありがとう、 ほんとうにありがとう!

4

4 に答える 4

2

Chrome Developer Tools、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

条件の順序が正しくありません。

現状:

  1. 最初のチェックは、両方の入力が空のシナリオを処理するために、これが期待どおりに機能することです。
  2. 次のチェックは、値の 1 つが空でないことです。
  3. 次のチェックは、他の値が空でないことです
  4. 次に、この時点で両方の入力が空ではないと想定します(else条件)

これを考慮してください: 条件 1 は、両方の入力が空でないことを保証します。これは、論理的には、それらの少なくとも 1 つが空でないことを意味します。これに続いて、それらの少なくとも 1 つが空でないことがわかっている場合、条件 2 または条件 3は常に と評価されtrueます。この結果、4 番目のコード パスは実行されません。

これを解決するには、両方の入力が明示的な条件として設定されていないことを確認してください。

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 に答える
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 に答える