将参数传递给函数

时间:2015-05-08 06:33:38

标签: javascript php

我想将参数传递给function。如果我将ID传递给参数它正在工作。但我想将字符串值作为参数传递给函数。我想将转义序列添加到参数中。如果我将$fet['cf_id']作为参数传递它就可以了。如果我传递$fet['file_name']值,则不会传递。

$file1=$fet['file_name'];
$ef=$fet['cf_id'];
$next1 = basename($file1);
echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png" alt="delete" style="width:10px;height:10px" title="Remove" onclick="myFunction('.$fet['file_name'].');"></td></tr>';
function myFunction(cid) {   
    // alert(cid);
    var  rmvfile=cid;
    //display conformation box
    if (confirm("Are you sure you want to Delete the file?") == true) {
        if(cid!='')
        {
            $.ajax({
                type:'post',
                url:'delete_cli_file.php',
                data:{rmvfile: rmvfile},
                success:function(msg){
                    if (msg.length> 0) {
                        alert(msg);
                        location.reload();
                    }
                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:1)

将文本传递给javascript时,它必须位于“text”中。

       $file1 =$fet['file_name'];
       $a     ="'";
       $ef    =$fet['cf_id'];
       $next1 = basename($file1);
       echo '<td style="text-align:center;width:100px;">
             <img src="image/delete1.png" alt="delete" 
              style="width:10px;height:10px" 
              title="Remove"
              onclick="myFunction('.$a.$fet['file_name'].$a.');">
             </td></tr>';

     function myFunction(cid)
     {   
        // alert(cid);
        var  rmvfile=cid;
        //display conformation box
        if (confirm("Are you sure you want to Delete the file?") == true)       
        {
         if(cid!='')
         {
           $.ajax({
           type:'post',
           url:'delete_cli_file.php',
           data:{rmvfile: rmvfile},
           success:function(msg)
           {
             if (msg.length> 0) 
             {
                alert(msg);
                location.reload();
             }
           }
      });
      }} }

答案 1 :(得分:0)

而不是

echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png"
      alt="delete" style="width:10px;height:10px" title="Remove"
      onclick="myFunction('.$fet['file_name'].');"></td></tr>'; 

使用

echo '<td style="text-align:center;width:100px;"><img src="image/delete1.png"
      alt="delete" style="width:10px;height:10px" title="Remove"
      onclick="myFunction(\''.$fet['file_name'].'\');"></td></tr>'; 
相关问题