javascript中的验证错误

时间:2012-08-14 01:51:57

标签: javascript

我想检查我的应用程序的按钮是否被按下。我面临的错误是,即使单击按钮,所有警报也会显示。附件是代码片段,通过单击按钮设置变量。

如果选择了任何值,我希望警报不显示

var condition ; 
var clickable;     // GLOBAL VARIABLES

function clickMe1() 
{
    clickable = "Sell";
}

function clickMe2() 
{
    clickable = "Rent";
}

function condition1()
{
    condition="Excellent"
}

function condition2()
{
    condition="Good"
}

function condition3()
{
    condition="Fair"
}

function condition4()
{
    condition="New"
}



function display()    
{
    if (condition != "Excellent"||"New"||"Fair"||"Good")
    {
        alert( " Please enter the condition ");
    }
    if (clickable != "Sell"||"Rent")
    {
        alert("Please enter the Sell");
    }
    if (costSell === '')  
    {
        alert("Please select a Price ");
    }
    if ((condition === "Excellent"||"New"||"Fair"||"Good") && (clickable === "Selling"||"leasing")&&(!isNaN(costSell)))
    {
        // Do Something
    },
    error: function(data){
        console.log("not added");
    }
    });
    }
    else
    {
        alert(" price is not a number"); 
    }
}

我也尝试过:

if(condition !='Excellent'|| condition!='New' || condition!='Fair'|| condition!='Good')
{
    alert( " Please enter the condition ");
}
if (clickable !='Sell'||'Rent' )
{
    alert("Please enter the Sell ");

3 个答案:

答案 0 :(得分:2)

if(condition !='Excellent'|| condition!='New' || condition!='Fair'|| condition!='Good')

应该是

if (condition != 'Excellent' && condition != 'New' && condition != 'Fair' && condition != 'Good')​

因为如果条件是优秀,新的,公平或良好的条件,则会触发您的版本。当条件其中之一时,将触发更正后的行。

并且

if (clickable !='Sell'||'Rent' )

应该是

if (clickable !='Sell' && clickable !='Rent' )

因为您只能使用clickable一次的快捷方式。

答案 1 :(得分:0)

condition !="Excellent"||"New"||"Fair"||"Good"

这样的条件是你的问题。

condition !="Excellent" && condition != "New" ...

^解决方案

答案 2 :(得分:0)

您的问题是您正在测试多个条件而不重复左侧操作数。

例如:

condition !="Excellent"||"New"||"Fair"||"Good"

应该是这样的:

condition != "Excellent" || condition != "New" || condition != "Fair" || condition !="Good"