简体   繁体   English

在Javascript函数中添加变量

[英]Adding variables in Javascript's function

I am trying to clone an td and inserting it to another table adding a new column.. I achieved it, but I can't assign properly a name.. I need to use 2 variables. 我正在尝试克隆td并将其插入到添加新列的另一张表中。我实现了,但是我不能正确分配名称。我需要使用2个变量。

console.log("Store Id: "+storeId+"///Product Id:"+newId);

var cloned = $(self).parents('tr').clone();
cloned.find('td').eq(5).after('<td class=""><input type="text" name="product_order_${storeId}_${newId}" id="order_input_cat" value="" class="input-text no-changes" /></td>');  
cloned.appendTo("#products #productOnCatGrid_table tbody");

As you can see, the variables are "storeId" and "newId". 如您所见,变量是“ storeId”和“ newId”。 The problem is that I can't add it into the name field of <input type.... 问题是我无法将其添加到<input type....的名称字段中。

Thanks a lot, and sorry my bad english :) 非常感谢,对不起我的英语不好:)

Change the single quotes (') to backticks (`) which will cause an interpolation of those two variables. 将单引号(')更改为反引号(`),这将导致对这两个变量进行插值。

Specifically: 特别:

.after(`<td class=""><input type="text" name="product_order_${storeId}_${newId}" id="order_input_cat" value="" class="input-text no-changes" /></td>`);

See here if you need more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals 如果需要更多信息,请参见此处: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals

For a quick demo run this in the console: 为了快速演示,请在控制台中运行此命令:

var x = 123
console.log(`What's the number? ${x} is the number!`)

Note: ES6 and up. 注意:ES6及更高版本。 Check your browser compat. 检查您的浏览器兼容性。 Ignore users on older browsers. 忽略旧版浏览器上的用户。 VIVA LA ES2016! VIVA LA ES2016!

If you want to bend over for the older browsers, change this: 如果您想改用较旧的浏览器,请更改此设置:

...name="product_order_${storeId}_${newId}"...

To this: 对此:

...name="product_order_' + storeId + '_' + newId + '"...

sad face 悲伤的脸

You can concatenate strings 您可以串联字符串

            cloned.find('td')
                  .eq(5)
                  .after('<td class=""><input type="text" name="product_orden_' 
                       + storeId 
                       + '_' 
                       + newId 
                       + '" id="orden_input_cat" value="" class="input-text no-changes" /></td>'); 

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

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