js调用表单值到变量

发布于 2024-11-19 01:00:39 字数 978 浏览 1 评论 0原文

好吧,这个问题很无聊。但已经让我总结了。

我在函数中使用 Js 脚本声明隐藏表单字段的值。

例如,这些只是示例,而不是真正的脚本。

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



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

  }

然后我希望能够在另一个脚本中使用表单值作为变量。我意识到可以选择全局声明这些变量。但我听说它不太安全。或者像我想要的那样精简。

第二个脚本示例...

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

    ...do stuff with variables.

但是这不起作用并返回未定义。这是访问表单字段值的正确 DOM 路径还是我需要查找属性及其子属性?

我还有什么其他选择?

感谢您抽出时间。 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> 

Ok noobablicious question. But has had me sumped.

Im declaring the value of a hidden form field with a Js script with in a function.

e.g. 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??

What other options are available to me??

Thanks for your time. HTML is...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-柠檬树下少年和吉他 2024-11-26 01:00:39

假设您已为所需元素设置了正确的 ID,那应该没问题。请记住,ID 必须是唯一的,否则会出现不可预测的问题。

确保在加载 DOM 后正在运行代码。否则,该元素可能尚不存在于 DOM 中,因此 document.getElementById 方法将无法找到它。

That should be fine, assuming that you have the correct ID's set to the elements you want. Remember, that ID's are required to be unique, or unpredictable issues will arise.

Make sure that you are running your code, after the DOM is loaded. Otherwise the element might not yet exist in the DOM, and so the document.getElementById method will fail to find it..

辞别 2024-11-26 01:00:39

或者,您可以将该数据存储在闭包中,以便两个函数都可以访问该变量,但它不存储在全局范围中。像这样:

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

此外,您还可以在第一个函数中声明变量,然后将变量作为参数传递到第二个函数,如下所示:

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

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. 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 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()
土豪 2024-11-26 01:00:39

我认为您在页面加载时没有设置启动脚本。如果是这样,您可以使用这个简单的事件处理程序:

window.onload = myFunction;

使用 myFunction 将与您上面的代码一起使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文