<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事件,并再次使用相同的按钮单击和方法来获取输入数据。我附上了我的代码,但无法正常工作。

最佳答案

您在这两行中误拼了getElementById。除此之外,代码对我来说似乎还不错。

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




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);
  }
}

<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>

09-20 13:35