jQuery 如何在javascript中连接字符串中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16302941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:54:18  来源:igfitidea点击:

How to concatenate variable in string in javascript

javascriptjqueryconcatenation

提问by user2320325

I am using this

我正在使用这个

var abc = jQuery('#pl_hid_addon_name').val(); 
alert(abc);

var atLeastOneIsChecked = jQuery('input[name="addon-"'+abc+']:checked').length ;
alert(atLeastOneIsChecked);

But it not worked It should be after concatenate like below

但它不起作用它应该像下面这样连接后

var atLeastOneIsChecked = jQuery('input[name="addon-base-bar2[]"]:checked').length;

回答by epascarello

var atLeastOneIsChecked = jQuery('input[name="addon-"'+abc+']:checked').length;
                                                    ^
                                                    |

You used the closing " at the wrong place

你在错误的地方使用了结束“

var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length;
                                                           ^
                                                           |

回答by A. Wolff

Concatenate like this:

像这样连接:

var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length ;

回答by Mohammad Adil

Try this -

尝试这个 -

var atLeastOneIsChecked = jQuery("input[name='addon-"+abc+"']:checked").length ;

回答by Jesus Flores

I tried some of the proposed above methods. However, no one was useful to me. After looking some information, I found the property attribute in jQuery (prop()). And that one works for me, the code is the following:

我尝试了上面提出的一些方法。然而,没有人对我有用。查阅了一些资料后,我在jQuery(prop())中找到了property属性。那个对我有用,代码如下:

In my JSF file, I had the following code.

在我的 JSF 文件中,我有以下代码。

<h:selectOneMenu id="asignacion" value="#{contratosBean.asignacion}">
<f:selectItems value="#{contratosController.asignaciones}"
        var="item" itemLabel="#{item.lblAsignacion}"
        itemValue="#{item.idAsignacion}" />
<f:ajax onevent="showDialog()" />

JavaScript section:

JavaScript 部分:

function showDialog() { if($([id='formContrato:asignacion']").prop("selected",true).val() == 'X1') { alert("function X1"); }else if($("id='formContrato:asignacion']").prop("selected",true).val() == 'X2'){alert("function X2"); }else{alert("Other function");} }