最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unable to get form elements by using form name in javascript - Stack Overflow

programmeradmin2浏览0评论

I am having an issue with accessing my form by it's name. When I use document.form[0].mylink.value it recognizes the value and outputs to the innerHTML that I specify. However, when I use document.myform.mylink.value it doesn't seem to recognize the form. I have tried this in chrome 6.0 and also firefox 3.6.3 and get the same result in both. I really need to be able to access my form values by using the form name (instead of document.form[0]), any ideas why document.myform.mylink.value doesn't work?

<form name="myform">
<table>
<tr><td valign="middle" align="center">
    <div id="textResponse"></div>
</td></tr>
<tr><td height="30" valign="middle" align="center">
    <input name="mylink" value="Enter a URL" size="31" />
</td></tr>
<tr><td valign="top" align="center">
    <a href="javascript:submitForm2();">Click</a>
</td></tr>
</table>
</form>

<script type="text/javascript">
function submitForm2(){
    //This one does NOT work:
    my_link = document.myform.mylink.value;
    //This one also does NOT work:
    //my_link = document.forms['myform'].mylink.value;
    //This one works:
    //my_link = document.forms[0].mylink.value;

    document.getElementById("textResponse").innerHTML="<p>"+my_link+"</p>";
}
</script>

I am having an issue with accessing my form by it's name. When I use document.form[0].mylink.value it recognizes the value and outputs to the innerHTML that I specify. However, when I use document.myform.mylink.value it doesn't seem to recognize the form. I have tried this in chrome 6.0 and also firefox 3.6.3 and get the same result in both. I really need to be able to access my form values by using the form name (instead of document.form[0]), any ideas why document.myform.mylink.value doesn't work?

<form name="myform">
<table>
<tr><td valign="middle" align="center">
    <div id="textResponse"></div>
</td></tr>
<tr><td height="30" valign="middle" align="center">
    <input name="mylink" value="Enter a URL" size="31" />
</td></tr>
<tr><td valign="top" align="center">
    <a href="javascript:submitForm2();">Click</a>
</td></tr>
</table>
</form>

<script type="text/javascript">
function submitForm2(){
    //This one does NOT work:
    my_link = document.myform.mylink.value;
    //This one also does NOT work:
    //my_link = document.forms['myform'].mylink.value;
    //This one works:
    //my_link = document.forms[0].mylink.value;

    document.getElementById("textResponse").innerHTML="<p>"+my_link+"</p>";
}
</script>
Share Improve this question edited May 5, 2012 at 19:00 Facundo Casco 10.6k8 gold badges44 silver badges65 bronze badges asked Oct 13, 2010 at 19:24 bassnoodlebassnoodle 1011 gold badge1 silver badge5 bronze badges 2
  • 1 Your code is certainly not XHTML (Transitional) valid. action is a required attribute for form elements. It should at least be set, even if it's empty. – Lekensteyn Commented Oct 13, 2010 at 19:28
  • i'm a little late but the reason it's not working might be because you're missing the "type" attribute. I know by default it's text, not sure if it's actually mandatory though. – I wrestled a bear once. Commented Jul 18, 2013 at 7:46
Add a comment  | 

1 Answer 1

Reset to default 20

Technically what you have should work ok... the full syntax below should also work:

var my_link = document.forms['myform'].elements['mylink'].value;

If by chance your variable in your "real" code is "mylink" not "my_link" you will get failures in IE as it will auto-reference the form element, not the value you are trying to extract.

That all said, your code as posted... works just fine in Firefox/IE (demo here: http://jsfiddle.net/Ymg8W/ )

发布评论

评论列表(0)

  1. 暂无评论