我是JavaScript的新手,但我遵循了一个非常好的教程,我在弹出窗口中签名,但它不起作用......我的想法是你有一个用户名和一个密码,如果两者都正确,你将重定向到一个页面。我丢失了表单的HTML代码,但是输入字段的id必须是uName,密码字段是pWord。这是我的JavaScript代码:
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord");
}
// This line controls the uName and pWord.
if(uName = "Admin",pWord = "Admin"){
// This line creates a pop-up with the name.
alert("Welcome " + uName);
// Need a line to redirect to a new page
// Need a line with the else statement
// Need a line to say if something if not all fields are filled
}
用户名必须是Admin,密码为Admin。如果您没有输入正确的组合,则必须收到如下消息:"组合不正确。"。
答案 0 :(得分:3)
这是=
operator的作业:
if (uName = "Admin",pWord = "Admin") {
虽然您需要比较==
or ===
:
if (uName == "Admin" && pWord == "Admin") {
同样使用逗号,
operator是不正确的(这是正确的,但不是在这个地方)。您需要逻辑AND &&
。
if
检查内部的分配使得表达式(" Admin"字符串是真实的),使代码始终输入if-block。
答案 1 :(得分:3)
有几个问题:
uName
pWord
和=
&&
来匹配if语句的两个测试作为旁注:您永远不应该使用像这样硬编码的用户名和密码验证登录,它始终可供用户使用,您需要做的就是右键单击浏览器并查看源代码以查看用户名和密码 - 这个登录脚本可以用于学习javascript的工作方式,但不要在现实世界中实现这样的登录,除非它纯粹是一种爬虫类型的威慑
=
设置一个值,==
匹配值,===
匹配值和类型(字符串,对象,int)
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
// This line controls the uName and pWord.
if(uName == "Admin" && pWord == "Admin"){
// This line creates a pop-up with the name.
alert("Welcome " + uName);
// Need a line to redirect to a new page
// Need a line with the else statement
// Need a line to say if something if not all fields are filled
}
}
打击你的评论
// Need a line to redirect to a new page
window.location.href="/pagetoredirectto.html";
和
// Need a line with the else statement
if(uName == "Admin" && pWord == "Admin"){
alert("Welcome " + uName);
} else {
alert( "User and Pass do not match" );
}
这应该在你获得值之后才进行..所以完整的代码就在这个答案的底部
// Need a line to say if something if not all fields are filled
if( !uName || !pWord )
{
alert( "Enter both a username and password" );
return;
}
我们在这里使用return
来阻止函数的其余部分执行
完整代码
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
// Need a line to say if something if not all fields are filled
if( !uName || !pWord )
{
alert( "Enter both a username and password" );
return;
}
// This line controls the uName and pWord.
if(uName == "Admin" && pWord == "Admin"){
alert("Welcome " + uName);
window.location.href="/pagetoredirectto.html";
} else {
alert( "User and Pass do not match" );
}
}
答案 2 :(得分:1)
如下所示更改您的代码并检查:
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
if(uName != "" && pWord != ""){ /* Ensure both fields have value */
if(uName == "Admin" && pWord == "Admin"){
alert("Welcome " + uName);
/* Redirection code goes here */
}else{
/* If user is not Admin */
}
}else{
/* Validation message goes here */
}
}
答案 3 :(得分:1)
多数民众赞成我希望:
function myFunction(){
var uName = document.getElementById("uname").value;
var pWord = document.getElementById("pWord").value;
if (uName == "Admin" && pWord == "Admin") {
// This line creates a pop-up with the name.
alert("Welcome " + uName);
// Need a line to redirect to a new page
document.location = "http://www.google.de"
// Need a line to say if something if not all fields are filled
}else if (uName == "" || pWord == "") {
alert("Fill all fields");
// Need a line with the else statement
}else{
alert("Incorrect Combination");
}
}

<input id="uname"></input>
<input id="pWord"></input>
<input type="button" onclick="myFunction()" value="Login"></input>
&#13;
仍然是一种不安全的登录脚本版本的方法。
如果你去调试,你可以看到用户名并传递密码短文不安全。
答案 4 :(得分:0)
您正在参考var pWord
中的特定整个对象,但您打算只取密码字段的值。
此外,您使用单个uName
为“{1}}和pWord
分配”管理员“。为了进行比较,请使用=
或===
,但建议使用==
。
这样做:
===
使用重定向
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
}
答案 5 :(得分:0)
试试这个例子:
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
// This line controls the uName and pWord.
if(uName == "Admin" && pWord == "Admin"){
// This line creates a pop-up with the name.
alert("Welcome " + uName);
// Need a line to redirect to a new page
location.href = "http://www.SOMEURL.com";
// Need a line with the else statement
} else {
// Need a line to say if something if not all fields are filled
alert("Wrong input");
}
}