简体   繁体   中英

Passing a java variable to a javascript function

I have the following piece of code which is a input content for a js lib:

content:'<a href="x.com" onclick="confirmDel("<%=user.name%>")" > <img src="delete.png" alt="Delete" width="15px" height="15px"></a></div>'

confirmDel(name)
{
 if(confirm("delete " + name +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}

when I click the delete link, it does not prompt me with the delete confirmation alert! what am I missing here? Is it about the way I pass the variable to the function?

 content:'<a href="x.com" onclick="confirmDel(\''+<%=user.name%>+'\')" > <img src="delete.png" alt="Delete" width="15px" height="15px"></a></div>'

onclick..use转义字符中传递参数的问题

confirmDel(name)
{
 if(confirm("delete " + name +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}

This is incorrect. Its halfway between declaring a function and calling it, but doesn't really do either.

You need to declare the function and then call it to execute that code. You're calling it in the onclick section now, but you need to define it correctly

function confirmDel(nameStr)
{
 if(confirm("delete " + nameStr +" ?"))
   {
     // Do delete stuff
   } else
   {
   return false;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM