javascript 无需提交即可获取文本字段值 (html)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8397105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get textfield value without submit (html)
提问by nimrod
Can I get the value of a text field in html without a form and without submit?
我可以在没有表单和提交的情况下获取 html 中文本字段的值吗?
I currently have a form on my website and inside this form I have a textfield. Unfortunately I can't have 2 forms at the same time (but I actually need two seperate actions), so I am looking for a way to get the value of the textfield without a submit.
我目前在我的网站上有一个表单,在这个表单中我有一个文本字段。不幸的是,我不能同时拥有 2 个表单(但我实际上需要两个单独的操作),所以我正在寻找一种无需提交即可获取文本字段值的方法。
All help is appreciated!
感谢所有帮助!
回答by Pablo
Try document.getElementById("textfield-id").value
尝试 document.getElementById("textfield-id").value
回答by remi bourgarel
document.getElementById('my_txt_box_id').value
where your text box 's id is "my_txt_box_id"
您的文本框的 id 是“my_txt_box_id”
Does that fit your need ?
这符合您的需要吗?
回答by T.J. Crowder
You can get a reference to the text field element (via getElementById
or any of several other DOM mechanisms), and then look at its value
property.
您可以获得对文本字段元素的引用(通过getElementById
或其他几种 DOM 机制中的任何一种),然后查看其value
属性。
Separately, if your form
element has a name
, and your input element has a name, you can access them via window
: window.formName.textFieldName.value
I don't think this is covered by an active spec (yet), but it probably will be at some stage (probably by an evolution of this draft specor a replacement of it) as it's nearly universally supported.
另外,如果您的form
元素有一个name
,并且您的输入元素有一个名称,您可以通过window
以下方式访问它们:window.formName.textFieldName.value
我认为活动规范尚未涵盖此内容,但它可能会处于某个阶段(可能由这个草案规范的演变或它的替代品),因为它几乎得到普遍支持。
References:
参考:
Alternately:
交替:
...I can't have 2 forms at the same time (but I actually need two seperate actions).
...我不能同时拥有 2 个表单(但我实际上需要两个单独的操作)。
You can have two submit buttons with the same name but different values, and differentiate between which one was clicked on the server (the value for the one that was clicked will be sent). So if you have two submits called "command" and one has the value "Foo" and the other "Bar", clicking the "Foo" button sends command=Foo
to the server along with the various form fields, whereas clicking the "Bar" button sends command=Bar
to the server with the other fields.
您可以拥有两个名称相同但值不同的提交按钮,并区分在服务器上单击了哪个按钮(将发送被单击的那个按钮的值)。因此,如果您有两个名为“command”的提交,一个的值为“Foo”,另一个为“Bar”,单击“Foo”按钮将command=Foo
与各种表单字段一起发送到服务器,而单击“Bar”按钮发送command=Bar
到服务器与其他字段。
回答by Stefan Koenen
Maybe you can use this jQuery / AJAX to submit the form without refreshing te page?
也许您可以使用此 jQuery / AJAX 提交表单而无需刷新页面?
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
else you can use javascript:
否则你可以使用javascript:
document.getElementById('id').value