jquery, undefined variable problem, how to?
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
精彩评论