简体   繁体   English

Javascript,并在括号符号中使用变量而不是字符串

[英]Javascript and using variables instead of strings in bracket notation

I have a PHP script that takes a text list with a variable number of questions and generates HTML form code that looks like this. 我有一个PHP脚本,该脚本接受带有可变数量问题的文本列表,并生成如下所示的HTML表单代码。

What did you think of X?<br />
<input type="hidden" name="$survey[0][0]" value="What did you think of X?">
<input type="radio" name="$survey[0][1]" value="1">Didn't like it.
<input type="radio" name="$survey[0][1]" value="2">I was indifferent.
<input type="radio" name="$survey[0][1]" value="3">Liked it.

$survey[n][0] is the question, $survey[n][1] is the answer, and n is the variable number of questions. $survey[n][0]是问题, $survey[n][1]是答案, n是可变数目的问题。

To validate using Javascript, I have a loop that loops through the questions, and a loop inside it that makes sure each question has an answer. 为了使用Javascript进行验证,我有一个循环遍历问题的循环,并且在其中有一个循环确保每个问题都有答案。 My problem is referencing the elements with [] in their names. 我的问题是引用名称中带有[]的元素。 Here's what I think is the relevant part of my code. 我认为这是代码的相关部分。

var formElements = document.forms["form"].elements;
var groupCount = document.getElementsByTagName("li").length;
var groupNdx = 0;
var groupName = "";
var btnCount = 0;
while (groupNdx < groupCount) {
  groupName = "'$survey[" + groupNdx + "][1]'";
  btnCount = formElements[groupName].length;

That last line doesn't work because formElements[groupName] is "undefined." 最后一行不起作用,因为formElements[groupName]是“未定义”。 formElements['$survey[3][1]'] works just fine, but that hardcodes the element's name, and I'd need to repeat the code for each question, and worse, it's a variable number of questions. formElements['$survey[3][1]']正常工作,但是硬编码元素的名称,我需要为每个问题重复代码,更糟糕的是,问题的数量是可变的。

As I was typing, the Similar Questions sidebar suggested I read Why aren't variable names converted to strings when using bracket notation in javascript? 在输入时,“类似问题”侧栏建议我阅读“ 为什么在javascript中使用方括号表示法时,变量名未转换为字符串? , so now I that's just how it is in Javascript. ,所以现在我就是Java脚本。

But then what workaround would you suggest? 但是您会建议什么解决方法? I could just forget about validating with Javascript since I also validate the form with PHP, but I understand it's good practice to validate forms at both the client and server sides. 我只是忘了使用Java脚本进行验证,因为我也使用PHP来验证表单,但是我知道在客户端和服务器端都验证表单是一种好习惯。 Thanks for your help! 谢谢你的帮助!

You have an extra set of single quotes that you shouldn't here: 您还有一些不应在此处使用的单引号:

groupName = "'$survey[" + groupNdx + "][1]'";

That adds single quotes into the key itself. 这会将单引号添加到密钥本身中。 Change that to: 更改为:

groupName = "$survey[" + groupNdx + "][1]";

Notice that x["'key'"] is a different from x['key'] in Javascript. 请注意, x["'key'"] x['key']与Java语言中的x['key']不同。 The first has a key of 'key' (including the quotes) while the second has just the string: key 第一个具有键'key' (包括引号),而第二个仅具有字符串: key

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

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