在jQuery中获取inputbox的值

时间:2017-09-25 08:03:38

标签: javascript jquery

我想获取并提醒input textbox的内容,但如果有人帮助我,它无效吗?

$(document).ready(function() {
  var phoneField = $("#PhoneField").val();
  $("#SendPhone").click(function() {
    alert(phoneField);
  });
});
<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>

7 个答案:

答案 0 :(得分:6)

您只在文档准备就绪时分配phoneField变量一次,这意味着您的phoneField将始终为空。在点击事件功能中分配变量,它应该有效。

$(document).ready(function() {
  $("#SendPhone").click(function () {
    var phoneField = $("#PhoneField").val();
    alert(phoneField);
  });
});
<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>

答案 1 :(得分:1)

在点击事件功能之后分配变量,它应该有效。 试试这个:

$(document).ready(function() {
  $("#SendPhone").click(function () {
    var phoneField = $("#PhoneField").val();
    alert(phoneField);
  });
});

答案 2 :(得分:1)

问题是您在pageload上设置了var phoneField,在页面加载时输入字段为空。

您可以通过更新onclick事件中的var来轻松解决此问题。

$(document).ready(function() {
  var phoneField = $("#PhoneField").val();
  $("#SendPhone").click(function () {
    phoneField = $("#PhoneField").val(); //updates the variable
    alert(phoneField);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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" />

答案 3 :(得分:1)

您希望获得点击事件的价值,然后您应该在点击事件中获得价值。

  $(document).ready(function() {
        
        $("#SendPhone").click(function () {
            var phoneField = $("#PhoneField").val();
            alert(phoneField);
        })
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.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" />

答案 4 :(得分:1)

单击按钮时获取文本字段的值。目前,它在页面加载时初始化一次。

$(document).ready(function() {
  $("#SendPhone").click(function () {
    alert($("#PhoneField").val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="PhoneField">
<input type="submit" id="SendPhone" value="Send">

答案 5 :(得分:0)

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


$(document).ready(function() {
  //var phoneField = $("#PhoneField").val();
  $("#SendPhone").click(function() {
    var phoneField = $("#PhoneField").val();
    alert(phoneField);
  });
});

您应该在click事件执行时获取值。

答案 6 :(得分:0)

<input type="text" name="" value="" id="PhoneField" />
<input type="button" name="next" class="next action-button" value="Next" id="SendPhone" />

<script type="text/javascript">`$("#SendPhone").click(function() {
        var a = $("#PhoneField").val()
        alert(a)
    })`

</script>