简体   繁体   English

当表单字段的名称带有方括号时,如何使用Javascript强制将光标移至特定表单字段?

[英]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.

相关问题 如何使用HTML5 / Javascript强制光标始终位于一个表单字段中? - How do I force cursor to always be in one form field using HTML5/Javascript? 在表单字段中强制光标 - Force cursor in form field 如果名称字段中包含。(点),如何使用angularjs验证表单? - How to validate the form using angularjs when the name field has .(dot) in it? 如何在Odoo JavaScript上检查何时打开某个表单视图并且字段已更改? - How can I check when certain form view is opened and field has changed in it on Odoo JavaScript? Shopify:单击按钮时如何将产品名称添加到输入字段表单? - Shopify: how do I get the Product Name to the Input Field form when the button is clicked? 如何使用slice javascript函数将字符串注入到表单字段中 - How do I use the slice javascript function to inject a string into a form field 如何使用JavaScript验证表单字段? - How to use javascript to validate form field? Javascript表单验证:如何强制将焦点保持在“不正确”字段上? - Javascript form validation: how to force focus to remain on 'incorrect' field? 当我在表单字段中输入文本时,JavaScript在表单中中断 - JavaScript breaks in form when I enter text in the form field 如何使用正则表达式和JavaScript强制用户在输入字段中输入特定模式 - How do I force user to enter specific pattern in input field using regex and javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM