简体   繁体   English

如何使用js指定结合文字和for循环计数器的输入ID?

[英]How to specify an input id combining a literal and a for loop counter using js?

I have several js for "loops" and I need the cleanest syntax to specity an id value using the "for loop counter".我有几个用于“循环”的 js,我需要最简洁的语法来使用“for 循环计数器”指定一个 id 值。 Here is an example that works, but it looks kludgy and it doesn't thrill me much to have to use such janky code.这是一个可行的示例,但它看起来很笨拙,并且不得不使用这样的乱码并没有让我感到很兴奋。 See that:看到:

"USCF_ID' +i +'"' +' "USCF_ID' +i +'"' +'

// USCF ID # // USCF ID #

items += '<td style="display:none;">
<input onblur="showID();" name = "ID" id = "USCF_ID' +i +'"' +' name = "USCF_ID" /> </td>';

Can you make a suggestion about expressing ID = USCF_ID1 programically in plain js?你能提出一个关于在纯 js 中以编程方式表达 ID = USCF_ID1 的建议吗? No jQuery if you can avoid it, please.如果可以避免,请不要使用 jQuery。

It was kind of difficult for me to follow along with your question, but what I got from your explanation is that you're just trying to find a cleaner way to concatenate your data.我很难理解你的问题,但我从你的解释中得到的是,你只是想找到一种更干净的方式来连接你的数据。 I would recommend that you use string templates here.我建议您在此处使用字符串模板 Here's an example:这是一个例子:

items += `<td style="display:none;">
<input onblur="showID();" name = "ID" id = "USCF_ID${i}" name = "USCF_ID" /> </td>`;

Creating a string with the `...` syntax creates a string template literal that allows you to insert JavaScript code between the ${ } curly brackets and concatenate the result.使用 `...` 语法创建字符串会创建一个字符串模板文字,允许您在${ }大括号之间插入 JavaScript 代码并连接结果。

You can use a string template where "USCF_ID' +i +'"' +' can be replaced by ${id} .您可以使用字符串模板,其中"USCF_ID' +i +'"' +'可以替换为${id} String templates are defined using a backtick character - ` :字符串模板使用反引号字符 - `定义:

items += `
  <td style="display:none;">
    <input onblur="showID();" name = "ID" id = "USCF_ID${id}" name = "USCF_ID" />
  </td>`;

Additionally you can extract the template into a function:此外,您可以将模板提取到函数中:

function getItem(id) {
  return `
  <td style="display:none;">
    <input onblur="showID();" name = "ID" id = "USCF_ID${id}" name = "USCF_ID" />
  </td>`;
}

Then you can generate your list as:然后,您可以将列表生成为:

items += getItem(id);

JS itself doesn't have built-in string formatting like other languages (with an exception later). JS 本身并没有像其他语言那样内置字符串格式(稍后例外)。 You can simulate it yourself in a simple case like this with the replace method:您可以使用replace方法在这样的简单情况下自己模拟它:

template = '<td style="display:none;"><input onblur="showID();" name="ID" id="USCF_ID$i" name="USCF_ID"/></td>';
...
items += template.replace("$i", i);

ES6 has template strings that can do this, but may not be available depending on the browser you are trying to support (note the backticks instead of single quotes): ES6 具有可以执行此操作的模板字符串,但可能不可用,具体取决于您尝试支持的浏览器(注意反引号而不是单引号):

items += `<td style="display:none;"><input onblur="showID();" name="ID" id="USCF_ID${i}" name="USCF_ID"/></td>`;

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

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