开发者

jquery, undefined variable problem, how to?

开发者 https://www.devze.com 2023-04-02 00:24 出处:网络
i hav开发者_运维问答e this situation: <div class=\"cont_picture\"> <?php echo \"<a id=\\\"mine_click\\\" href=\\\"#\\\" >a test</a>\"; ?>

i hav开发者_运维问答e this situation:

<div class="cont_picture">
<?php echo "<a id=\"mine_click\" href=\"#\" >a test</a>"; ?>
</div>
<div id="number"><?php echo $number; ?></div>

this will give me something like: a test 123456 (the link is as an example only)

then i have the jquery:

$('#mine_click').live('click', function() {
    var strtalentnum = $('#number').text();
    $('#mine').trigger('click');
})

and;

if($strtalentnum){
alert ($strtalentnum);
}

but the alert doesnt work.

Any ideas how to get this working? see jsfiddle

thanks


You have defined strtalentnum inside #mine_click click event handler and try to access it outside the handler which is out of scope. If you want to access it outside the handler then define it in the outer scope or glbally.

Also the variable you are trying to alert begins with $. I think its a typo.

Working demo

var strtalentnum;

$('#mine_click').live('click', function() {
    strtalentnum = $('#number').text();
    $('#mine').trigger('click');
});

$('#mine').click(function(){
   if(strtalentnum){
       alert (strtalentnum);
   }
}); 


Put the if inside the function() and beware that you are writing $strtalentnum but the variable is called strtalentnum

0

精彩评论

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