使用javascript在HTML表单中自动填充一个字段。

问题描述 投票:0回答:1
    <li>
            <div class="destination">
                  <img src="image/destination/chamba.jpg" alt="">
              <div class="destinationDetails">
                <h2>Chamba <h2>
                <h3>Starting From Rs. 2500 <h3>
                  <button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" type="button" name="button" class="commonBtn"> Book Now</button>
                  <button type="button" name="button" class="commonBtn"> View Variations</button>
              </div>
            </div>
          </li>

          <li>
            <div class="destination">
                  <img src="image/destination/parvati.jpg" alt="">
              <div class="destinationDetails">
                <h2>Parvati Valley <h2>
                <h3>Starting From Rs. 2500 <h3>
                  <button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" type="button" name="button" class="commonBtn"> Book Now</button>
                  <button type="button" name="button" class="commonBtn"> View Variations</button>
              </div>
            </div>
          </li>

<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
  <form class="modal-content" action="/action_page.php">
    <div class="container">
      <h1>Sign Up</h1>
      <p>Please fill in this form to create an account.</p>
      <hr>
      <label for="email"><b>Email</b></label>
      <input type="text" placeholder="Enter Email" name="email" required>


       <label><b>Package</b></label>

<input type = "text" id="test2" name "teste2">

<button type="button" onclick="MyFunction()">Click To Fill Up</button>

      <label for="psw"><b>Password</b></label>
      <input type="password" placeholder="Enter Password" name="psw" required>

      <label for="psw-repeat"><b>Repeat Password</b></label>
      <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

      <label>
        <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
      </label>

      <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

      <div class="clearfix">
        <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
        <button type="submit" class="signupbtn">Sign Up</button>
      </div>
    </div>
  </form>
</div>
<script>
// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script>

我使用这个代码使一个预订页面。我想填写的包领域自动当有人点击 "现在预订 "或使用一个按钮,以填补该领域的点击。

例如:如果我点击 "立即预订",我想自动填写套餐字段,当有人点击 "立即预订 "或使用按钮填写字段。如果我点击 "现在预订 "按钮,在第一个 "利",包填写应自动填写为 "尚巴",如果我点击 "现在预订 "按钮,在第二个 "利 "的包领域应自动填写为 "帕尔瓦蒂谷"。

有什么办法可以做到这一点...?

提前感谢您的帮助。

javascript html css web-deployment
1个回答
0
投票

添加一个函数并设置适当的值。就当是学习用的,仅供参考。

var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

function setPackage(packageName) {
  document.querySelector('#test2').value = packageName;
}
<li>
  <div class="destination">
    <img src="image/destination/chamba.jpg" alt="">
    <div class="destinationDetails">
      <h2>Chamba
        <h2>
          <h3>Starting From Rs. 2500
            <h3>
              <button onclick="setPackage('Chamba');document.getElementById('id01').style.display='block'" style="width:auto;" type="button" name="button" class="commonBtn"> Book Now</button>
              <button type="button" name="button" class="commonBtn"> View Variations</button>
    </div>
  </div>
</li>

<li>
  <div class="destination">
    <img src="image/destination/parvati.jpg" alt="">
    <div class="destinationDetails">
      <h2>Parvati Valley
        <h2>
          <h3>Starting From Rs. 2500
            <h3>
              <button onclick="setPackage('Parvati Valley');document.getElementById('id01').style.display='block'" style="width:auto;" type="button" name="button" class="commonBtn"> Book Now</button>
              <button type="button" name="button" class="commonBtn"> View Variations</button>
    </div>
  </div>
</li>

<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
  <form class="modal-content" action="/action_page.php">
    <div class="container">
      <h1>Sign Up</h1>
      <p>Please fill in this form to create an account.</p>
      <hr>
      <label for="email"><b>Email</b></label>
      <input type="text" placeholder="Enter Email" name="email" required>


      <label><b>Package</b></label>

      <input type="text" id="test2" name "teste2">

      <button type="button" onclick="MyFunction()">Click To Fill Up</button>

      <label for="psw"><b>Password</b></label>
      <input type="password" placeholder="Enter Password" name="psw" required>

      <label for="psw-repeat"><b>Repeat Password</b></label>
      <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

      <label>
        <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
      </label>

      <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

      <div class="clearfix">
        <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
        <button type="submit" class="signupbtn">Sign Up</button>
      </div>
    </div>
  </form>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.