Javascript 为什么只有第一个是有效的?

Javascript 为什么只有第一个是有效的?,javascript,if-statement,Javascript,If Statement,基本上,我已经设置好了它,这样当人们输入代码时,它会将他们重定向到正确的页面。然而,由于某些原因,只有第一个if起作用。其他的格式相同,但不起作用。有什么想法吗 编辑:为了更清楚,底部的两个代码可以很好地工作,这样就什么都没有了。它会告诉您输入一个RSVP代码,但我不能在顶部添加超过这两个代码的代码 编辑2:所以如果我尝试输入“77050”,它只会说代码无效。这就是我所说的“不工作”的意思 编辑3:我上面还有这个代码 <script type="text/javascript">

基本上,我已经设置好了它,这样当人们输入代码时,它会将他们重定向到正确的页面。然而,由于某些原因,只有第一个if起作用。其他的格式相同,但不起作用。有什么想法吗

编辑:为了更清楚,底部的两个代码可以很好地工作,这样就什么都没有了。它会告诉您输入一个RSVP代码,但我不能在顶部添加超过这两个代码的代码

编辑2:所以如果我尝试输入“77050”,它只会说代码无效。这就是我所说的“不工作”的意思

编辑3:我上面还有这个代码

<script type="text/javascript">

    var today = new Date();
    var seminarDate = new Date();
    seminarDate.setFullYear(2013, 10, 15);

    if ( today >= seminarDate )
            {
                window.alert("Seminar is over, sorry!");
            }

    </script>

var today=新日期();
var seminarDate=新日期();
第二年(2013年10月15日);
如果(今天>=seminarDate)
{
注意(“研讨会结束了,对不起!”);
}

函数myFunction()
{
var pincode=document.getElementById(“pincode”).value;

如果(pincode==“76638”&&today您必须定义today和seminarDate

   function myFunction(today, seminarDate) { // ?

        if ( today > seminarDate ) {
            window.alert("Seminar is over.");
            return;
        }

        var pincode = document.getElementById("pincode").value;

        if ( pincode === "" ) {
            document.getElementById("error").innerHTML = "Please enter your RSVP code.";
            return;
        }

        var pincodes = {
            "76638": "http://google.com",
            "76265": "http://google.com",
            "77050": "http://google.com",
            "77283": "http://google.com",
            "77329": "http://google.com"
        };

        if ( pincodes[pincode] !== undefined ) {
            window.location.assign(pincodes[pincode]);
            return;
        }

        document.getElementById("error").innerHTML = "Code is invalid.";

    }

因此,根据我所看到的,我假设
seminarDate
可能是从服务器填充的(这就是为什么它被列为如下所示):

这很好。但是为了清理其中的一些(可能是优化的),下面的例子怎么样:

var today = new Date();
var seminarDate = new Date();
seminarDate.setFullYear(2013, 10, 15);
// setup list of valid pincodes and their links
var validCodes = {
    '76638': 'http://google.com/',
    '76265': 'http://google.com/',
    '77050': 'http://google.com/',
    '77283': 'http://google.com/',
    '77329': 'http://google.com/'
};

// refactor the check out in to a reusable function so we can call it
// on page load and from `myFunction`
function seminarHasEnded()
{
    if ( today >= seminarDate )
    {
        window.alert("Seminar is over, sorry!");
        return true;
    }
    return false;
}
seminarHasEnded();

// refactored version of myFunction
function myFunction()
{
    //
    // 1st test--can we apply?
    //
    if ( seminarHasEnded() ) {
        return;
    }

    // store a couple of values and references. Also reset the error
    // box on every new attempt
    var pincode = document.getElementById('pincode').value,
        err = document.getElementById('error');
    err.textContent = '';

    //
    // 2nd test--did they enter something?
    //
    if ( !pincode || pincode.length == 0 ){
        err.textContent = 'Please enter your RSVP code.';
        return;
    }

    //
    // 3rd test--is the code valid?
    //

    // check pincode exists/is valid
    if ( validCodes[pincode] ){
        alert('You would have been redirected to ' + validCodes[pincode]);
        //window.location.assign(validCodes[pincode]);
    } else {
        err.textContent = 'Code is invalid';
    }
}
这将导致如下结果:

然而,有几点需要注意:

  • 当您发现自己在多个IF语句中添加了相同的条件时,是时候退后一步,看看如何以不同的方式完成
  • 我使用了
    innerHTML
    ,但这可能是个人偏好
  • 这仍然是JavaScript,因此任何查看页面的人都会看到有效的PIN和URL。应该访问。我建议使用表单提交或AJAX来执行此测试/查找,以获得无缝体验

你说的“不工作”是什么意思?我会把前4个else if和第一个if放在一个开关中。另外,这个函数有什么输入?今天的
是什么
以及
pincode
的值是什么?当你使用===时,确保你知道你有什么变量类型(不会发生类型转换,这可能是原因之一)…使用===和ahhhhh这么多代码背后的原因是什么!使用哈希来保存有效的PIN和urlshmm>{}[“forEach”]==未定义的falseAh,“forEach”…我在做一个PHP项目。这不是一个很好的例子,但是如何保证他们输入的是一个数字而不是一个对象的普通属性。Wont{}['toString']是否定义?我假设OP使用
http://google.com
作为虚拟数据。您需要将URL放在
pincodes
对象中,而不是
true
表达式。顺便说一句,为什么投反对票?
// setup list of valid pincodes and their links
var validCodes = {
    '76638': 'http://google.com/',
    '76265': 'http://google.com/',
    '77050': 'http://google.com/',
    '77283': 'http://google.com/',
    '77329': 'http://google.com/'
};

// refactor the check out in to a reusable function so we can call it
// on page load and from `myFunction`
function seminarHasEnded()
{
    if ( today >= seminarDate )
    {
        window.alert("Seminar is over, sorry!");
        return true;
    }
    return false;
}
seminarHasEnded();

// refactored version of myFunction
function myFunction()
{
    //
    // 1st test--can we apply?
    //
    if ( seminarHasEnded() ) {
        return;
    }

    // store a couple of values and references. Also reset the error
    // box on every new attempt
    var pincode = document.getElementById('pincode').value,
        err = document.getElementById('error');
    err.textContent = '';

    //
    // 2nd test--did they enter something?
    //
    if ( !pincode || pincode.length == 0 ){
        err.textContent = 'Please enter your RSVP code.';
        return;
    }

    //
    // 3rd test--is the code valid?
    //

    // check pincode exists/is valid
    if ( validCodes[pincode] ){
        alert('You would have been redirected to ' + validCodes[pincode]);
        //window.location.assign(validCodes[pincode]);
    } else {
        err.textContent = 'Code is invalid';
    }
}