js调用表单值到变量
好吧,这个问题很无聊。但已经让我总结了。
我在函数中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已为所需元素设置了正确的 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..或者,您可以将该数据存储在闭包中,以便两个函数都可以访问该变量,但它不存储在全局范围中。像这样:
此外,您还可以在第一个函数中声明变量,然后将变量作为参数传递到第二个函数,如下所示:
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:
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:
我认为您在页面加载时没有设置启动脚本。如果是这样,您可以使用这个简单的事件处理程序:
使用 myFunction 将与您上面的代码一起使用。
I think that you haven't set starting script when page load. If so, you can use this simple event handler:
With myFunction will be function with yours above code.