开发者

Getting inputs inside of a form tag

开发者 https://www.devze.com 2023-02-19 07:58 出处:网络
I have a form with id and multiple inputs with ids as well how i get a specific input inside a form tag.

I have a form with id and multiple inputs with ids as well how i get a specific input inside a form tag.

<form action="#" method="post" id="frm-location">
      <input type="text" name="txt-location" id="txt-location" />
</form>

what I want is to get the txt-location from th开发者_开发百科e frm-location


You want a reference to the <input> element itself?

var form = document.getElementById('frm-location'),
    input = form.getElementsByTagName('input');

// or, more specifically:
var form = document.getElementById('frm-location'),
    input = form['txt-location'];
    // if the name didn't have a dash in it, you could write this instead:
    input = form.txtLocation;

// or, even better, since the input has an ID:
var input = document.getElementById('txt-location');
  • https://developer.mozilla.org/en/DOM/document.getElementByID
  • https://developer.mozilla.org/en/DOM/element.getElementsByTagName
  • https://developer.mozilla.org/en/HTML/element/form
  • https://developer.mozilla.org/en/DOM/element

You want the value of that element?

var input = /* whatever */,
    inputValue = input.value;
  • https://developer.mozilla.org/en/DOM/HTMLInputElement


HTML is a static type markup language. As such, by itself there are not many options for accessing and processing data. There are a few general approaches for getting data from a web page. I'll keep the explainations generic, but they will translate to whatever platform/language you are using.

  • Access the data server side. This is accomplished by having the user submit the form. Once submitted, the values will be available via the query parameters. Various languages will have different methods to access the parameters.

  • Access the data client side. You can always use javascript to hook client side events like onblur, onchange, onfocus. Once your javascript fires, you can access various form elements with dom/js methods like getElementById/getElementByName -- Which would be able to reference your form elements but Id/Name respectively.

  • A Hybrid approach. AJAX is a mixture of the two approaches listed above. Client side code (javascript) makes async calls to the server. the server then processes the data in some manner and sends responses back to the client.

Hope this points you in the right direction. If you would like to clarify your question a bit, I can certainly try to cater the answer more to your specific case.

0

精彩评论

暂无评论...
验证码 换一张
取 消