如何使用单击按钮更改占位符并在同一按钮单击事件上捕获新数据

时间:2017-08-15 03:56:51

标签: javascript html

<div id="abc" class="first">
           <input type="text" placeholder="Number" id="no" class="contact">
           <input type="text" placeholder="Pass"  id="pass" class="password">
 <div class="btn1" role="group">
         <button type="button" onclick="change()" class="btnregi">Register 
        </button>
  </div>  

    ----------
    javascript function
    var entry=0;
    function change()
        {

         if(entry==0)
      {
       document.getElementById("no").placeholder = "Enter user name...";
       document.getElementById("pass").placeholder = "Enter user password";
       entry++;
      }
       else 
       {
          var newuser=document.getelementById("no").value;
          var newpass=document.getelementById("pass").value;
          alert("you entered new username"+newuser);
          alert("you entered new password"+newpass);
       }   

我想更改占位符值onclick事件,并再次使用相同的按钮单击和方法我想要获取输入数据。我附上了我的代码,但它无法正常工作。

1 个答案:

答案 0 :(得分:1)

你在这两行中拼错了getElementById。除此之外,代码似乎对我很好。

var newuser = document.getElementById("no").value;
var newpass = document.getElementById("pass").value;

&#13;
&#13;
var entry = 0;

function change() {

  if (entry == 0) {
    document.getElementById("no").placeholder = "Enter user name...";
    document.getElementById("pass").placeholder = "Enter user password";
    entry++;
  } else {
    var newuser = document.getElementById("no").value;
    var newpass = document.getElementById("pass").value;
    alert("you entered new username: " + newuser);
    alert("you entered new password: " + newpass);
  }
}
&#13;
<div id="abc" class="first">
<input type="text" placeholder="Number" id="no" class="contact">
<input type="text" placeholder="Pass" id="pass" class="password">
<div class="btn1" role="group">
  <button type="button" onclick="change()" class="btnregi">Register 
        </button>
</div>
&#13;
&#13;
&#13;