w3c document.forms [0] .fieldname等效 [英] w3c document.forms[0].fieldname equivalent

查看:249
本文介绍了w3c document.forms [0] .fieldname等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用

  document.forms [0] .fieldname.value 

可以从表单中获取javascript中的值,但是我想用名称来引用该字段而不是0,自< form name =formname> 不符合要求之后,这些日子应该是相同的吗?

解决方案最简单的方法是在输入中添加一个id:< input id =fieldname/> 并引用该值这样的JavaScript:

  document.getElementById('fieldname')。value 

或者,如果您使用jQuery

  $ ( '#fieldname')VAL(); 


I've been using

document.forms[0].fieldname.value

to get values in javascript from a form, but I'd like to use a name to reference the field and not a 0, what would be the equivalent these days since <form name="formname"> isn't compliant?

解决方案

The easiest way is to add an id to the input: <input id="fieldname" /> and reference the value from JavaScript like so:

document.getElementById('fieldname').value

or, if you're using jQuery

$('#fieldname').val();

这篇关于w3c document.forms [0] .fieldname等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆