表单元素值未存储在javascript变量中

时间:2016-09-14 08:09:33

标签: javascript html forms bootstrap-modal

我一直在通过构建一些模型网站来学习javascript,同时这样做会弹出一个错误,表单元素没有存储到我的javascript变量中。我已经插入了包含表单和JS的模态对话框的HTML代码code.Can有人建议我哪里出错了?

document.getElementById("sub").addEventListener("click",store());
function store(){
	var	userName = document.getElementById("nme").value;
	var	emailId = document.getElementById("mail").value;
	var	password = document.getElementById("pd").value;
	var	rpassword = document.getElementById("rpd").value;
	console.log(userName);
	    if (password !== rpassword){
		  alert("your password doesn't match");
	    }
	    else{
		
			localStorage.setItem("name",userName.value);
			localStorage.setItem("email",emailId.value);
			localStorage.setItem("password",password.value);
		}
	}
<div class=row>
  <form class="col-xs-12" method="post">
    <div class="form-group">
      <input type="text" class="form-control form-design input-lg enable" id="nme" name="user" placeholder="UserName">
      <input type="text" class="form-control input-lg enable" id="mail" name="mail" placeholder="Email-Id">
    </div>
    <div class="form-group">
      <input type="password" class="form-control form-design input-lg enable" id="pd" name="pwd" placeholder="Password">
      <input type="password" class="form-control input-lg enable" name="rpwd" id="rpds" placeholder="Retype Password">
      <input type="submit" id="sub" class="btn btn-block btn-lg navi-style lnk" value="SignUp">
    </div>
  </form>
</div>
<footer class="align">Already a member?<a class="newlnk" data-toggle="modal" href="#login">SignIn</a>
</footer>
</div>
<!--body closed-->
</div>
<!--content closed-->
</div>
<!--Dialog box closed-->
</div>
<!--Model closed-->
</div>
<!--container closed-->

2 个答案:

答案 0 :(得分:0)

您正在尝试存储userName.value而不是userName(依此类推)。 此外,您添加了一个事件侦听器,但实际上调用了函数存储而不是引用它。

document.getElementById("sub").addEventListener("click",store);
function store(){
    var userName = document.getElementById("nme").value;
    var emailId = document.getElementById("mail").value;
    var password = document.getElementById("pd").value;
    var rpassword = document.getElementById("rpd").value;
    console.log(userName);
        if (password !== rpassword){
          alert("your password doesn't match");
        }
        else{

            localStorage.setItem("name",userName);
            localStorage.setItem("email",emailId);
            localStorage.setItem("password",password);
        }
    }

答案 1 :(得分:0)

要存储的变量应该是string类型,即var userName应该已经是一个字符串,因此存储userName.value将导致尝试存储'undefined',因此要存储的正确值应该是没有'value的var '属性和你的元素ID之一是错误的,即

var rpassword = document.getElementById("rpds").value;

e.g。尝试以下代码而不是当前存储函数

function store(){
	var	userName = document.getElementById("nme").value;
	var	emailId = document.getElementById("mail").value;
	var	password = document.getElementById("pd").value;
	var	rpassword = document.getElementById("rpds").value;
	console.log(userName);
	    if (password !== rpassword){
		  alert("your password doesn't match");
	    }
	    else{
		
			localStorage.setItem("name",userName);
			localStorage.setItem("email",emailId);
			localStorage.setItem("password",password);
		}
	}