[英]How do I use Javascript to force cursor to specific form field when form field has a name with brackets?
I am using CakePHP as my framework. 我正在使用CakePHP作为我的框架。
On page load, I want to force the cursor to a specific form field where name="data[Project][title]" 在页面加载时,我想强制将光标移动到name =“ data [Project] [title]”的特定表单字段
I'm trying to use javascript: 我正在尝试使用javascript:
This works fine if I change the name to something without brackets, but fails to work with this form name. 如果我将名称更改为不带方括号的名称,则效果很好,但无法使用此表单名称。 I have to use this form field name because of how CakePHP processes my form data. 由于CakePHP如何处理表单数据,我必须使用此表单字段名称。
Is there a workaround or another simple way to force the cursor to this form field? 是否有解决方法或另一种简单的方法来将光标强制到此表单字段?
Here is the code I currently have (if you change "data[Project][title]" to "formField" it works): 这是我当前拥有的代码(如果将“ data [Project] [title]”更改为“ formField”,则可以使用):
<body onLoad="document.searchForm.data[Project][title].focus();">
<form action="http://beta.industrialinterface.com/users/mainadd/" method="post" id="create-form" name="searchForm">
<input id="main-input" type="text" class="title-limit" name="data[Project][title]" onClick="this.value='';limitText(60);" onKeyDown="limitText(60);return true;" onKeyUp="limitText(60);return true;" />
Example code? 示例代码? Because otherwise you could simply do: 因为否则您可以简单地执行以下操作:
document.getElementById('id_for_your_input').focus();
If you are using a form and have access to the form element you can do 如果您使用的是表单并且可以访问form元素,则可以执行
formelement['data[project][title]'].focus();
example at http://www.jsfiddle.net/fqgxv/ http://www.jsfiddle.net/fqgxv/上的示例
You can use getElementsByName()
for this. 您可以为此使用getElementsByName()
。 Note that it returns an array. 请注意,它返回一个数组。
Ex: 例如:
<body onLoad="document.getElementsByName("data[Project][title]")[0].focus();">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.