Javascript 我的代码不起作用,怎么了?
我是JavaScript新手,但我遵循了一个非常好的教程,我正在制作一个登录弹出表单,但它不起作用。。。这个想法是,你有一个用户名和一个密码,如果两者都正确,你将重定向到一个页面。我丢失了表单的HTML代码,但是有一个输入字段,id必须是uName,密码字段pWord。以下是我的JavaScript代码:Javascript 我的代码不起作用,怎么了?,javascript,html,forms,popup,Javascript,Html,Forms,Popup,我是JavaScript新手,但我遵循了一个非常好的教程,我正在制作一个登录弹出表单,但它不起作用。。。这个想法是,你有一个用户名和一个密码,如果两者都正确,你将重定向到一个页面。我丢失了表单的HTML代码,但是有一个输入字段,id必须是uName,密码字段pWord。以下是我的JavaScript代码: function myFunction(){ var uName = document.getElementById("uName").value; var pWord =
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。如果您没有输入正确的组合,您必须收到一条消息,如:组合不正确。这是一项分配:
虽然您需要比较:
另外,逗号的用法也不正确,它是正确的,但在这里不正确。您需要逻辑AND&
if check中的赋值使truthy表达式管理字符串为truthy,使代码始终输入if块。这是带有以下内容的赋值:
虽然您需要比较:
另外,逗号的用法也不正确,它是正确的,但在这里不正确。您需要逻辑AND&
if check中的赋值使truthy表达式管理字符串为truthy,使代码始终输入if块。有几个问题: 您获取的不是密码字段的值,而是对象 您没有验证函数中的凭据 您正在使用一个= 您需要使用&&来匹配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 to say if something if not all fields are filled
if( !uName || !pWord )
{
alert( "Enter both a username and password" );
return;
}
我们在这里使用一个return来停止函数的其余部分的执行
完整代码
有几个问题: 您获取的不是密码字段的值,而是对象 您没有验证函数中的凭据 您正在使用一个= 您需要使用&&来匹配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 to say if something if not all fields are filled
if( !uName || !pWord )
{
alert( "Enter both a username and password" );
return;
}
我们在这里使用一个return来停止函数的其余部分的执行
完整代码
虽然您只打算获取密码字段的值,但您正在引用var pWord中特定的整个对象 此外,您还使用single=为uName和pWord分配Admin。对于比较,使用===或==尽管我建议使用=== 这样做:
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
}
用于重定向
window.location = "http://www.location.com/ie.htm";
虽然您只打算获取密码字段的值,但您正在引用var pWord中特定的整个对象 此外,您还使用single=为uName和pWord分配Admin。对于比较,使用===或==尽管我建议使用=== 这样做:
function myFunction(){
var uName = document.getElementById("uName").value;
var pWord = document.getElementById("pWord").value;
}
用于重定向
window.location = "http://www.location.com/ie.htm";
按以下方式更改代码并检查:
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 */
}
}
按以下方式更改代码并检查:
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 */
}
}
我们试试这个例子:
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");
}
}
我们试试这个例子:
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");
}
}
我希望如此:
函数myFunction{
var uName=document.getElementByIduname.value;
var pWord=document.getElementByIdpWord.value;
如果uName==Admin&&pWord==Admin{
//此行将创建一个带有名称的弹出窗口。
欢迎+联塞特派团;
//需要一行重定向到新页面
document.location=http://www.google.de
//如果不是所有字段都已填充,则需要一行来说明是否有内容
}如果uName==| pWord=={
填写所有字段;
//需要一行else语句吗
}否则{
错误的组合;
}
}
我希望如此:
函数myFunction{
var uName=document.getElementByIduname.value;
var pWord=document.getElementByIdpWord.value;
如果uName==Admin&&pWord==Admin{
//此行将创建一个带有名称的弹出窗口。
欢迎+联塞特派团;
//需要一行重定向到新页面
document.location=http://www.google.de
//需要一句话来说明如果没有
t所有字段都已填充
}如果uName==| pWord=={
填写所有字段;
//需要一行else语句吗
}否则{
错误的组合;
}
}
你得到的是输入pWord的完整对象而不是它的值吗?你得到的是输入pWord的完整对象而不是它的值吗?你认为ifuName==Admin,pWord==Admin{会起作用吗?@suresponnukalai在你的评论之前我已经更新了我的答案:-:好。注意到了。你认为ifuName==Admin,pWord==Admin吗{will work???@suresponnukalai在你发表评论之前,我已经更新了我的答案:-:很好。注意到了。你不想解释你为什么这么做吗?与其像问题陈述中所说的那样只说更改,它应该满足他的要求。检查null和冗余,因为在你检查管理平等性之后。但是如果,这段代码是在IE中测试的呢?如果字段是空的呢?IE有什么问题吗?如果字段是空的,它的值将是空的,它不能是空的。但是如果它是空的,那么check==Admin就足够了。你不想解释为什么要这样做吗?与其说对它进行更改,不如说它应该满足他的要求,就像问题陈述所说的那样。C检查空值和空值是多余的,因为在您还检查了管理员的相等性之后。但是如果,这段代码在IE中测试了呢?如果字段是空的呢?IE有什么问题吗?如果字段是空的,它的值就不能是空的。但是如果它是空的,检查==Admin就足够了。