2014-01-08 71 views
0

于是久违的替代值数,我已经做了广泛的研究,大多数的例子我见过之一:的getElementById似乎

  • 解释为什么可能会返回null

  • 该值意味着一个数字,需要从字符串转换为数字

我的代码正在进行的是该值是一个字符串,并且可以通过查看表单有一个有效的字符串条目进行可视化验证。该代码返回值为2,没有错误抛出。试图通过Chrome的控制台找到变量的值将返回undefined。但是,在同一个表单中,函数根据查看发送到PHP处理文件的内容生成一个正确传递的唯一标识。

下面是HTML片段:

<input type="text" id="billing-email"> 

此领域有一个有效的电子邮件项,即[email protected]

,是为了获取信息的JavaScript文件看起来是这样的:

$(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); 
}); 

发送到PHP文件处理看起来像这样的标题:

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而不是电子邮件地址?

+0

如何改变'document.getElementById'到'return $(“#billing-email”).val()'影响你的代码?因为你没有从该函数返回任何值。 – Jason

+8

因为您将setInterval ID返回给电子邮件,而不是您的getemail函数的结果。 – j08691

+5

'function getemail(){document.getElementById(“billing-email”)。value;}'什么都不做。 –

回答

1

正如j08691在评论中指出的,您正在存储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); 
}); 
+0

皮特,我编辑你的例子进一步澄清。 –

+0

虽然小记,把uuid = guid();在submitform()函数中会在每次发送时生成一个新的uuid。虽然我没有在原始描述中指定它,但uuid是唯一的。所以,我把它移回了submitform函数之外,现在一切正常,谢谢你的帮助。 –