jQuery Mobile:调用JavaScript函数ReferenceError:找不到变量

时间:2012-04-18 10:03:53

标签: jquery-mobile

如果使用此代码,我的代码将起作用:

for(i=0; i<listUsers.length; i++){
   if(listUsers[i] != usr){
      $( "#listHisUser" ).append('<li><a href="#hist_cont" onclick="getChatContent()">'+listUsers[i]+'</a></li>').listview('refresh');
   }
}

function getChatContent(){
   alert("data");
}

但如果我改成它:

for(i=0; i<listUsers.length; i++){
   if(listUsers[i] != usr){
      $( "#listHisUser" ).append('<li><a href="#hist_cont" onclick="getChatContent('+listUsers[i]+')">'+listUsers[i]+'</a></li>').listview('refresh');
      }
}

function getChatContent(data){
      alert(data);
      alert("data");
}

收到错误:ReferenceError: Can't find variable: tqminh at file:///android_asset/www/index.html#hist:1其中tqminh是我点击的listview项目。

<!-- History -->
<div data-role="page" id="hist" data-title="HISTORY">
   <div id="a" data-role="header" data-theme="b">
   <h1 style="text-align: left;font-size: 20px">History Chat</h1>
   </div>       
   <div data-role="content" data-inset="true">
   <ul data-role="listview" data-theme="c">
   <li><a href="#" onclick="getChatContent('tqminh')">tqminh</a></li>
   </ul>
   </div>
</div>

1 个答案:

答案 0 :(得分:0)

您的函数参数未呈现为字符串。尝试替换

<a href="#hist_cont" onclick="getChatContent('+listUsers[i]+')">'+listUsers[i]+'</a>

<a href="#hist_cont" onclick="getChatContent(\''+listUsers[i]+'\')">'+listUsers[i]+'</a>