admin管理员组

文章数量:1236560

Can I get the value of a text field in html without a form and without submit?

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.

All help is appreciated!

Can I get the value of a text field in html without a form and without submit?

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.

All help is appreciated!

Share Improve this question asked Dec 6, 2011 at 8:33 nimrodnimrod 5,73230 gold badges88 silver badges152 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

Try document.getElementById("textfield-id").value

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.

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 spec or a replacement of it) as it's nearly universally supported.

References:

  • DOM2 Core
  • DOM2 HTML Bindings
  • DOM3 Core
  • HTML5 APIs

Alternately:

...I can't have 2 forms at the same time (but I actually need two seperate actions).

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.

Maybe you can use this jQuery / AJAX to submit the form without refreshing te page?

http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

else you can use javascript:

document.getElementById('id').value
document.getElementById('my_txt_box_id').value

where your text box 's id is "my_txt_box_id"

Does that fit your need ?

本文标签: javascriptGet textfield value without submit (html)Stack Overflow