简体   繁体   English

js调用表单值到变量

[英]js call form value to variable

Ok noobablicious question.好的noobablicious问题。 But has had me sumped.但已经让我陷入了困境。

Im declaring the value of a hidden form field with a Js script with in a function.我在 function 中使用 Js 脚本声明隐藏表单字段的值。

eg These are just examples not the real script.例如,这些只是示例而不是真正的脚本。

function myFunction(){
    var text = "hello world";
    var number = 12345;



    document.getElementById('text').value= text; 
    document.getElementById('number').value= number; 

  }

Then I want to be able to use the value of the form value as a variable in another script.然后我希望能够将表单值的值用作另一个脚本中的变量。 I realize that there is the option to declare these variables globally.我意识到可以选择全局声明这些变量。 However I have heard that it is not as secure.但是我听说它不那么安全。 Or a streamlined as I am going for.或者我想要的流线型。

Second Script example...第二个脚本示例...

    var autoText = document.getElementById('text').value;
    var autoNumber = document.getElementById('number').value;

    ...do stuff with variables.

However this is not working and returns undefined.但是,这不起作用并返回未定义。 Is this the correct DOM path to access the value of my form fields or do I need to find an attribute and its child??这是访问我的表单字段值的正确 DOM 路径还是我需要找到一个属性及其子项?

What other options are available to me??我还有哪些其他选择?

Thanks for your time.谢谢你的时间。 HTML is... HTML 是...

  <form action="http://mysite/mypath" method="post">

  <input type="text" name="text" id="text" value="">
  <input type="text" name="text" id="number" value="">
  <input type= "submit" name="go" value="go" allign="middle"/>
  </form> 

That should be fine, assuming that you have the correct ID's set to the elements you want.这应该没问题,假设您将正确的 ID 设置为所需的元素。 Remember, that ID's are required to be unique, or unpredictable issues will arise.请记住,ID 必须是唯一的,否则会出现不可预知的问题。

Make sure that you are running your code, after the DOM is loaded.确保在加载 DOM 后运行代码。 Otherwise the element might not yet exist in the DOM, and so the document.getElementById method will fail to find it..否则该元素可能还不存在于 DOM 中,因此document.getElementById方法将无法找到它。

Or, you could just store that data in a closure so that both functions have access to the variable, but it's not stored in the global scope.或者,您可以将该数据存储在闭包中,以便两个函数都可以访问该变量,但它不存储在全局 scope 中。 Like so:像这样:

(function(){
  var text = "blah blah",
      number = 12345;

  function insertValues() {
    document.getElementById('text').value= text; 
    document.getElementById('number').value= number; 
  }
  function otherStuffWithValues() {
    alert(text);
    alert(number);
  }
  insertValues();
  otherStuffWithValues();
}())

Additionally, You could also declare the variables inside the first function, and then pass the variables onto the second function as a parameter like so:此外,您还可以在第一个 function 中声明变量,然后将变量作为参数传递给第二个 function,如下所示:

function insertValues() {
  var text = "blah blah",
      number = 12345;
  document.getElementById('text').value= text; 
  document.getElementById('number').value= number;
  otherstuff(text,number)
}

function otherstuff(sometext,somenumber) {
  alert(sometext);
  alert(somenumber);
}
insertValues()

I think that you haven't set starting script when page load.我认为您在页面加载时没有设置启动脚本。 If so, you can use this simple event handler:如果是这样,您可以使用这个简单的事件处理程序:

window.onload = myFunction;

With myFunction will be function with yours above code.使用 myFunction 将是 function 与您的上述代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM