基本上,我已将其设置为当人们将代码放入其中时,会将其重定向到正确的页面。但是,出于某种原因,只有第一个如果正在工作。其他的格式相同但不起作用。有任何想法吗?
编辑:为了更清楚,底部的两个工作正常,所以它没有任何东西它会告诉你输入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>
function myFunction()
{
var pincode = document.getElementById("pincode").value;
if ( pincode === "76638" && today <= seminarDate ){
window.location.assign("http://google.com");
}
else if ( pincode === "76265" && today <= seminarDate ){
window.location.assign("http://google.com");
}
else if ( pincode === "77050" && today <= seminarDate ){
window.location.assign("http://google.com");
}
else if ( pincode === "77283" && today <= seminarDate ){
window.location.assign("http://google.com");
}
else if ( pincode === "77329" && today <= seminarDate ){
window.location.assign("https://google.com");
}
else if ( pincode === "" ){
document.getElementById("error").innerHTML = "Please enter your RSVP code.";
}
else if ( today >= seminarDate )
{
window.alert("Seminar is over.");
}
else {
document.getElementById("error").innerHTML = "Code is invalid.";
}
};
</script>
答案 0 :(得分:1)
基于我所看到的,我假设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';
}
}
这会导致类似这样的事情:http://jsfiddle.net/dqpnh/
然而,有几点说明:
textContent
而不是innerHTML
,但这可以被视为个人偏好。答案 1 :(得分:0)
您必须定义今天和讲座日期。
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.";
}