【问题标题】:Fill Up A Field Automatically In HTML form using javascript使用 javascript 在 HTML 表单中自动填写字段
【发布时间】:2020-05-28 03:47:59
【问题描述】:
    <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>

我正在使用此代码制作预订页面。我想在有人点击“立即预订”或使用按钮通过点击填写字段时自动填写包裹字段。

例如:如果我点击第一个“li”中的“book now”按钮,填写的包裹应该自动填写为“Chamba”,如果我点击第二个“li”中的“book now”按钮, packages 字段应自动填写为“Parvati Valley”。

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

提前感谢您的帮助。

【问题讨论】:

  • 你在哪里展示包裹?我只能看到一个包含用于 id email 的文本包的标签
  • 意味着如果你点击按钮,除了h2 带有包名的标签外,所有东西都应该消失......对吗?
  • 每个
  • 都是一个包。并且 test id="test2" 应该取
  • 中的文本。它应该根据在哪个 li 下单击哪个 book now 按钮来获取文本。如果单击第一个 li 下的立即预订按钮,则 id="test2" 的输入应自动为“Chamba”,如果单击第二个 li 下的立即预订按钮,则 id="test2" 的输入应为“Parvati Valley”。

标签: javascript html css web-deployment


【解决方案1】:

添加一个函数并设置适当的值。仅出于学习目的考虑。

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>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签