doupu3635
2016-11-01 05:43
浏览 225

如何通过onclick传递字符串参数

I'm generating a table through ajax function. But I need to add onclick function.Inside this function passing two parameters one is number and other is name.

Controller

 $table.='  <td><input type="checkbox"  value="" id="sp_'.$app_id.'" onclick="SpDetails('.$app_id.','.$app_details->app_name.');" /></td>'

 echo  $table;

script

 function SpDetails(val,name)
 {
    alert(name)

 }

But I didn't get the name when I alert.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanpa1980 2016-11-01 06:11
    已采纳
    • You have extra " here id="sp_'.$app_id.'" ".
    • Pass the second parameter as string so you miss double quotes.
    • Internal double quotes should be escaped like this "foo \" foo".
    • You can pass variable in strings with double quotes "$variable".

    so your code should be like this

       $table.="  <td><input type='checkbox'  value='' id='sp_$a' onclick='SpDetails($a,\"$b\");' /></td>";
    
    
         echo $table;
    
    打赏 评论
  • douhu1990 2016-11-01 06:04

    try following:

     onclick="SpDetails('" .$app_id. "','" .$app_details->app_name. "');"
    
    打赏 评论
  • drnycqxwz63508434 2016-11-01 06:38

    your parameter has no quote so it throws error . Use \' for making parameter as a string .

    $table ='<td><input type="checkbox"  value="" id="sp_'.$app_id.'" onclick="SpDetails(\''.$app_id.'\',\''.$app_details->app_name.'\');" /></td>';
    

    That will write in html as

    <input onclick="SpDetails('app_id','app_name');"> //that is just example 
    

    Your orginal is

    <input onclick="SpDetails(app_id,app_name);">  //no quote so assume as variable
    
    打赏 评论

相关推荐 更多相似问题