开发者

jquery and/or javascript simplest issue (but can't figure it out)

开发者 https://www.devze.com 2023-01-08 01:42 出处:网络
Using jquery, The following line works i.e. radioClicks function gets called. $(\"input:[name=\'\"+54464+\"\']\").bind( \"click\", radioClicks );

Using jquery,

The following line works i.e. radioClicks function gets called.

$("input:[name='"+54464+"']").bind( "click", radioClicks ); 

but this one doesn't:

$("input:[name='"+options.rowId+"']").bind( "click", radioClicks );

and yes, you have guessed it, options.rowId = 54464 (at least in the debugger) .

What am I missing ???

Thanks

EDIT:

I removed the : as some suggested, I used alert(options.rowId) and it shows 54464 as expected. Also, it is not used in a loop. The code is:

function radioFormatter (cellvalue, options, rowObject)
{
    $("input[name='"+options.rowId+"']").bind( "click", radioClicks ); 

    if("checked" == cellvalue)
        return "<input type='radio' name='"+ name +"' id='"
               + options.colModel.name + "' value='" + options.rowId
               + "' checked>";

    return "<input type='开发者_如何学Goradio' name='" + name + "' id='" +options.colModel.name
           +"' value='" + options.rowId + "'>";
}

It is used with jqgrid where I have a row with multiple columns with a radio button in it.

I have tried everything I can think of with no success...

Thanks


I'm not sure why the first works actually, but the : is extra in there, it should just be this:

$("input[name='"+options.rowId+"']").bind( "click", radioClicks );

The : usually preceeds pseudo-classes or filters, but it's not needed for your attribute-equals selector.


Edit: As for the difference, are your sure options.rowId == 54464 at that time? Stick a console.log(options.rowId) just before that line and see what the result in Chrome or Firebug is.


I think the selector is correct. But without whole code, it's hard to tell the root cause.

My guess is that the code uses a loop to attach event listeners to a group of radio buttons and the author meets the infamous closure issue with loops. options.rowId always refers to the last value in the loop.

Anyway, need more details.


My guess would be options.rowId returns a int however the $("input[name='"+options.rowId+"']") requires a string

try

var idname = options.rowId;
$("input:[name='"+idname+"']").bind( "click", radioClicks );

or use firebug or chrome js debug to findout more


Aside from the colon being in the wrong spot this code is fine. I think your debugger is fooling you.

You have to be careful of references in your debug statements. If you just do console.log(options) the value in the debugger could change for options.rowId. Try doing an alert(options.rowId) before that bind call.

alert("input[name='"+options.rowId+"']");
$("input[name='"+options.rowId+"']").bind( "click", radioClicks );


the quoting around [attribute=] param is not strictly necessary, so

$("input[name=" + options.rowId + "]").bind( "click", radioClicks )

;


I have found the problem but don't know how to fix it:

This doesnt work:

var options = {rowId:1223};
$("input:[name="+options.rowId]+"]").bind( "click", function(){
    alert('clickedz');
});

But this does:

var options 1223;
$("input:[name="+options]+"]").bind( "click", function(){
    alert('clickedz');
});

I am not a js expert but the issue seems to be how the option object gets created.


It looks like you have a double quote.
Try this:

$("input:[name="+options.rowId+"]").bind( "click", radioClicks );  

It seems to be working. http://jsfiddle.net/EG2Yx/. Check your other codes?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号