5

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;
0

4 Answers 4

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;
                                                           ^
                                                           |
Sign up to request clarification or add additional context in comments.

Comments

2

Try this -

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

Comments

2

Concatenate like this:

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

1 Comment

The name's value will break the selector if there's any special characters like [, because you've removed quoting.
0

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");} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.