English 中文(简体)
当参数为整数但非字符串时, 单击函数上的 javascript 函数有效吗?
原标题:javascript onclick function works when parameter is integer but not string?
echo  <a onclick="load_tags( .$list[ id ]. )"></a> 

list[id] 是数字时, 它有效。 但是当 list[id] 是单词时, 它不起作用 。 为什么?

下面是有关我的职能和正在从事的工作的一些背景情况,尽管为了回答这个问题没有必要。

脚本脚本脚本

function load_tags(id){
        $.post( ../php/tags/let_tags.php 组合键 ,{id:id},function(data){
            $( #tags_selected ).text(data);
        });
    }

let_tags.php 组合键

$tag_id=$_POST[ id ];
    echo $tag_id;
    $users_with_this_tag=show_all_users_with_this_tag($tag_id);
    if(count($users_with_this_tag)!=0){
            foreach($users_with_this_tag as $key => $list){
                echo $list[ user_id ];
            }
    }else{
        echo Nobody with this tag ;
    }
最佳回答

只需绕过 load_tags 参数, 并使用逃逸的单引号 :

echo  <a onclick="load_tags(  .$list[ id ].  )"></a> ;
问题回答

字串应该有引号 围绕它们:

echo  <a onclick="load_tags(" .$list[ id ]. ")"></a> 

Becouse it couses syntax error. Calling JS function with int:

function(123) 
{
 ....
}

用字符串 :

function( foo ) 
{
 ....
}

没有 , 它会在字符串名称下搜索变量( 如果它是有效的变量名称 ) 。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签