简体   繁体   English

当文本框名称包含方括号时,Javascript 不起作用

[英]Javascript doesn't work when textbox name contains square brackets

I've got this script for calculating square area, which works perfect:我有这个计算正方形面积的脚本,它非常有效:

<script language="javascript" type="text/javascript">
function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.autoSumForm.firstBox.value;
  two = document.autoSumForm.secondBox.value; 
  document.autoSumForm.Area.value = (one * 1) * (two * 1);
}
function stopCalc(){
  clearInterval(interval);
}
</script>

<form name="autoSumForm">
<input  size="3"  type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();">
<input  size="3"  type=text name="secondBox" value="" onFocus="startCalc();" onBlur="stopCalc();">
<input  size="4"  type=text name="Area" readonly="true">
</form>

But my problem is that I need the name of the third box to be Area[<?php echo $area['area_id']; ?>]但我的问题是我需要第三个框的名称是Area[<?php echo $area['area_id']; ?>] Area[<?php echo $area['area_id']; ?>]

But I can't get the javascript to work, when I use brackets in the name.但是当我在名称中使用括号时,我无法让 javascript 工作。

Note that you should not add the brackets if you don't need them!请注意,如果不需要括号,则不应添加括号! It makes it unnecessary complex.它使它变得不必要的复杂。

In your posted code there is no indication that you need those brackets.在您发布的代码中,没有迹象表明您需要这些括号。 You normally add them if you have several input fields with the same name and you want PHP to create an array.如果您有多个具有相同名称的输入字段并且您希望 PHP 创建一个数组,则通常添加它们。 For more information please refer to Variables From External Sources .有关更多信息,请参阅来自外部来源的变量

In case you need them, you have to use bracket notation to access the field:如果您需要它们,您必须使用括号表示法来访问该字段:

document.autoSumForm['Area[<?php echo $area["area_id"]; ?>]'].value = (one * 1) * (two * 1);

Also I would suggest to not pass a value inside the brackets in the name of the field.另外我建议不要在字段名称的括号内传递值。 This would simplify your code to:这会将您的代码简化为:

<input  size="4"  type=text name="Area[]" readonly="true">

and:和:

document.autoSumForm['Area[]'].value = (one * 1) * (two * 1);

PHP will then create an array with continuous numerical keys starting at 0 .然后 PHP 将创建一个数组,其中包含从0开始的连续数字键。

While Felix's answer will work in most (all?) browsers, it should be noted that HTML names and IDs are supposed to be valid identifiers, which cannot contain square brackets. 虽然 Felix 的答案适用于大多数(所有?)浏览器,但应注意 HTML 名称和 ID 应该是有效标识符,不能包含方括号。 The correct solution is to not use square brackets in your names and IDs. 正确的解决方案是不要在您的姓名和 ID 中使用方括号。

EDIT: I stand corrected.编辑:我的立场得到纠正。 In HTML 4, The 'id' attribute is type ID, but the 'name' attribute for form elements is type CDATA, so can contain practically anything.在 HTML 4 中,“id”属性是类型 ID,但表单元素的“名称”属性是 CDATA 类型,因此几乎可以包含任何内容。 See http://www.w3.org/TR/html4/index/attributes.htmlhttp://www.w3.org/TR/html4/index/attributes.html

I was probably thinking of meta 'name', which is type NAME.我可能在想 meta 'name',它是 NAME 类型。

  1. Add id to textarea将 id 添加到 textarea

    <input size="4" type=text name="Area" id="someid" readonly="true">

  2. Put value by id按 id 放置值

    document.getElementById('someid').value = (one * 1) * (two * 1);

I would add an 'id' attribute to the third box (third input) and give the id a value without square brackets.我会在第三个框(第三个输入)中添加一个“id”属性,并给 id 一个不带方括号的值。

Use the 'id' attribute to select the DOM element in your javascript code.将“id”属性用于 select javascript 代码中的 DOM 元素。 And let the 'name' attribute keeps its square brackets.并让 'name' 属性保留其方括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM