最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to concatenate variable in string in javascript - Stack Overflow

programmeradmin1浏览0评论

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;

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;
Share Improve this question edited Apr 30, 2013 at 15:14 Rick Viscomi 8,8524 gold badges40 silver badges55 bronze badges asked Apr 30, 2013 at 14:56 user2320325user2320325 611 gold badge1 silver badge6 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 18
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;
                                                           ^
                                                           |

Try this -

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

Concatenate like this:

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

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:

In my JSF file, I had the following code.

<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:

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");} }
发布评论

评论列表(0)

  1. 暂无评论