在jQuery中获取inputbox的值

sep*_*ehr -1 javascript jquery

我想得到并提醒内容,input textbox但它无法正常工作有人可以帮助我吗?

$(document).ready(function() {
  var phoneField = $("#PhoneField").val();
  $("#SendPhone").click(function() {
    alert(phoneField);
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
  <h2 class="fs-title">Enter Your Number</h2>
  <h3 class="fs-subtitle">This is step 1</h3>
  <input type="text" name="email" placeholder="Phone number" id="PhoneField" />
  <input type="button" name="next" class="next action-button" value="Next" id="SendPhone" />
</fieldset>
Run Code Online (Sandbox Code Playgroud)

Fur*_*oss 6

phoneField当文档准备好时,您只分配变量一次,这意味着您phoneField将始终为空.在点击事件功能中分配变量,它应该工作.

$(document).ready(function() {
  $("#SendPhone").click(function () {
    var phoneField = $("#PhoneField").val();
    alert(phoneField);
  });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
  <h2 class="fs-title">Enter Your Number</h2>
  <h3 class="fs-subtitle">This is step 1</h3>
  <input type="text" name="email" placeholder="Phone number" id="PhoneField" />
  <input type="button" name="next" class="next action-button" value="Next" id="SendPhone" />
</fieldset>
Run Code Online (Sandbox Code Playgroud)