Javascript getElementById似乎返回的是数字而不是值
因此,我做了广泛的研究,我看到的大多数例子如下:Javascript getElementById似乎返回的是数字而不是值,javascript,Javascript,因此,我做了广泛的研究,我看到的大多数例子如下: 解释可能返回null的原因 或 该值是一个数字,需要从字符串转换为数字 我的代码中的情况是,该值应该是一个字符串,并且可以通过将表单视为具有有效的字符串条目来进行可视化验证。代码返回的值为2,没有抛出错误。试图通过Chrome的控制台找到变量的值返回undefined。但是,在同一个表单上,函数根据发送到PHP处理文件的内容生成一个正确传递的唯一id 以下是html代码片段: <input type="text" id="billi
- 解释可能返回null的原因
- 该值是一个数字,需要从字符串转换为数字
<input type="text" id="billing-email">
发送到PHP处理文件的头如下所示:
$(document).ready(function() {
function getemail(){document.getElementById("billing-email").value;}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
var email = window.setInterval(getemail,100);
var uuid = guid();
function submitform(){
$.ajax({
type: "POST",
url: 'https://app.batteryjunction.com/cr/previous-builds/jstorage-test/controller/process.php',
dataType: 'json',
data: {
myform:[{
uuid: uuid,
email: email,
}]
},
async: false,
});
}
window.setInterval(submitform,3000);
});
Request URL: https://some.com/some/app.php
Request Method: POST
Status Code: 200 OK
Form Data
myform[0][uuid]: ghjkdkgh-f456-75fd-5gsr-jgdk8vhd
myform[0][email: 2
问题是,为什么它发送的是2而不是电子邮件地址?正如在评论中指出的,您存储的是setInterval
ID(显然是2
)。下面是一个更好的方法:
$(document).ready(function () {
function getemail() {
// this function doesn't return anything, so "var email = getemail();" means "var email" would be undefined
document.getElementById("billing-email").value;
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
// window.setInterval returns an integer value representing the interval
// so you can use clearInterval to stop the interval
// correct use of "email" below would be: window.clearInterval(email);
//var email = window.setInterval(getemail, 100);
//var uuid = guid();
function submitform() {
var email = document.getElementById("billing-email").value,
uuid = guid();
$.ajax({
"type": "POST",
"url": "https://app.batteryjunction.com/cr/previous-builds/jstorage-test/controller/process.php",
"dataType": "json",
"data": {
"myform": [{
"uuid": uuid,
"email": email //remove the last comma, or IE will throw a hissy-fit
}]
},
"async": false,
});
}
window.setInterval(submitform, 3000);
});
将
document.getElementById
更改为return$(“#计费电子邮件”).val()
对您的代码有何影响?因为您没有从该函数返回任何值。因为您将setInterval ID返回到电子邮件,而不是getemail函数的结果。function getemail(){document.getElementById(“billing email”).value;}
不做任何事情。另外window.setInterval(getemail,100)
。这将返回一个“timeoutID”。一个数字。使用它可以使用clearTimeout
停止超时。您正在将email
设置为这个“timeoutID”,这就是您在AJAX调用中发送的内容some@some.com. j08691和Rocket Hazmat-将脚本更改为将var email=document.getElementById(“billing email”).value;在函数中,然后以间隔运行函数将返回一个错误,即电子邮件未定义。pete,我编辑了您的示例以进一步澄清。不过,请注意,将uuid=guid();函数的作用是:每次发送时生成一个新的uuid。虽然我没有在原始描述中指定它,但uuid是唯一的。因此,我将其移回submitform函数之外,现在一切正常,谢谢您的帮助。