[英]get value of element of html in another js file
在我的索引页面中,我发送了一个请求ajax,响应是一个数字。我将响应设置为具有隐藏类型的input的值
<input type="hidden" id="Status">
<script src="js/indexPhone.js"></script>
<script>
$(document).ready(function() {
$("#SendPhone").click(function(e) {
var phoneField = $("#PhoneField").val();
var phoneFieldString = "989" + phoneField.substring(0);
$.ajax({
type:'post',
url:'getSMS.php',
data:{'phoneFieldString':phoneFieldString},
success:(function (response) {
$("#Status").val(response);
})
})
});
});
</script>
我的问题是我想在我包含在索引中的另一个javascript页面中获取此值,但它警报为空。 这是我的indexPhone.js:
$("#SendPhone").click(function() {
alert($("#Status").val());
});
Ajax请求是异步的,因此第二次单击将在设置值之前运行。
使用事件:
// index.html
$("#SendPhone").click(function (e) {
// ...
$.ajax({
// ...
success: (function (response) {
$("#Status").val(response);
$("#Status").trigger("gotSMS");
})
});
});
// indexPhone.js
$("#Status").on("gotSMS", function () {
alert($(this).val());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.