在字段名称中使用带括号的Javascript [英] Using Javascript with Brackets in Field Name

查看:181
本文介绍了在字段名称中使用带括号的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用名称包含括号的HTML表单元素?

How do I reference a HTML form element whose name contains brackets?

例如,

<form name="myForm">
    <input type="checkbox" name="myElement[7]" />
</form>

我没有重命名表单元素的选项,我无法使用jquery。

I do not have the option of renaming the form element and I cannot use jquery.

如何使用javascript单独访问此元素?我已经尝试使用:

How would I access this element using javascript alone? I already tried using:

alert(document.myForm.myElement\\[7\\].type);

结果为未定义。我还尝试了上面的javascript,没有斜杠,没有斜杠,也有undefined。

The result is "undefined". I also tried the above javascript snipped without the slashes and also got "undefined".

javascript使用括号来访问一个字段的正确语法是什么? >

What is the correct syntax for javascript to access a field named with brackets?

推荐答案

我相信你在找:

alert( document.myForm.elements['myElement[7]'].type );

这篇关于在字段名称中使用带括号的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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