我正在尝试建立一个验证特定密码的网站,如果是这样,它会将用户带入我的网站,我无法解决该错误,所以我能得到一些帮助......?
<script language="Javascript">
function check(x){
if (x == "HI"){
alert("Just Press Ok to Continue...");
} else {
alert("Nope... not gonna happen");
}
}
var grape = alert("Just press Ok to Countinue...")
function alertIt() {
if (grape === true){
window.location="http://www.google.com";
} else {
alert("Nope... not gonna happen")
}
}
}
</script>
<center>
<br /><h3>Enter the Password</h3>
<br />
<b>Password:</b>
<input type="password" id="pass" value="">
<br /><br />
<button onclick="javascript:check(document.getElementById('pass').value)">Check Password</button>
<button onclick="javascript:alertIt">On To my Website</button>
谢谢你我只是个孩子,我的javascript仍然很粗糙,我不想使用PHP。
答案 0 :(得分:1)
char matrix[maxNumberofState][maxNumberofState];
int tempInt;
char tempChar;
getline(file,line);
stringstream valuesOfLine(line);
valuesOfLine >> state>> acceptingBool >> tempInt >> tempchar;
matrix[state][tempInt] = tempChar;
valuesOfLine >> tempInt >> tempChar;
while(!matrix[state][tempInt]){
matrix[state][tempInt] = tempChar;
valuesOfLine >> tempInt >> tempChar;
}
将等于未定义。
这是因为grape
总是返回undefined(没有返回值)。
您想改用alert()
。
答案 1 :(得分:1)
您的代码的主要问题是您有两个功能 完成一件事,你的第二个功能(
alertIt
)是基于 一个永远不会成真的条件。
让我们一次解决一个问题......
}
之前还有一个额外的 </script>
标签。这将导致您的代码甚至无法执行。让&#39; S
希望这只是一个复制/粘贴错误。var grape = alert("Just press Ok to
Countinue...")
将始终导致grape
成为undefined
因为 alert()
永远不会返回值。那就是
在您的if (grape === true){
中,您无法预料:alertIt
功能永远是真实的。check
函数。
你可以摆脱alertIt
函数和&#34;到我的
网站&#34; 按钮。onclick
,onmouseover
,onmouseout
等)。他们
导致创建全局匿名包装函数
spaghetti代码使得扩展和调试代码更加困难
请勿关注 W3C DOM Event standards 。<center>
)。因此,您现在调整工作代码就像这样。请阅读代码中的注释,以解释正在做什么以及为什么:
// When the DOM is fully loaded:
window.addEventListener("DOMContentLoaded", function(){
// Get references to the elements that you'll need
var password = document.getElementById("pass");
var btnPassword = document.getElementById("btnCheck");
// Then, instead of wiring HTML elements to JavaScript event callbacks in HTML
// do it with the W3C DOM Event standard of: .addEventListener()
btnPassword.addEventListener("click", function(){
// Call your check function and pass it the password
check(password.value);
});
// This is the only function you need. It combines the old "alertIt" with "check"
function check(x){
if (x == "HI"){
// If the password is correct, tell the user and proceed
alert("Just Press Ok to Continue...");
window.location="http://www.google.com";
} else {
// If not, just tell the user:
alert("Nope... not gonna happen");
}
}
});
&#13;
/* Write style rules to affect the parts of the document you want */
body { text-align:center; }
#pwd { font-weight:bold; }
&#13;
<!-- Note that <center> and <b> have been moved into the CSS
because styling should not be done in HTML. Also, note that
<div> and <p> were added for structure and excessive <br> were
removed. Again, don't use HTML for formatting. -->
<div>
<h3>Enter the Password</h3>
<br>
<span id="pwd">Password:</span>
<input type="password" id="pass">
</div>
<p>
<button id="btnCheck">Check Password</button>
</p>
&#13;
答案 2 :(得分:-1)
这非常适合学习,但任何人都可以查看来源并查看您的密码,所以它只是为了学习... Mmmkay?我已经删除了所有其他逻辑,因此我们可以专注于这个问题。一旦你解决了@ScottMarcus和其他人提到的所有问题,你应该得到类似的东西......
function check(x) {
if (x === "HI") {
alert('going to website')
location.assign("#mywebsite");
} else {
alert("Nope... not gonna happen");
}
}
&#13;
<h3>Enter the Password</h3>
<b>Password:</b>
<input type="password" id="pass" value="">
<button onclick="javascript:check(document.getElementById('pass').value)">Check Password</button>
&#13;