Javascript onclick不调用JS函数(在任何浏览器上)
例如:Javascript onclick不调用JS函数(在任何浏览器上),javascript,jquery,Javascript,Jquery,例如: <button id="banButton{$id}"onclick="deletePhoto({$id}, {$picture_path});">BAN {$id}</button> BAN{$id} 在标记之前: <script type="text/javascript" src="../profile/theme/js/jquery.js"></script> <script type="text/javascript"
<button id="banButton{$id}"onclick="deletePhoto({$id}, {$picture_path});">BAN {$id}</button>
BAN{$id}
在
标记之前:
<script type="text/javascript" src="../profile/theme/js/jquery.js"></script>
<script type="text/javascript">
function deletePhoto(id, pic_path) {
alert("test");
$.post("ajax.php", { toid: toid, pic_path: pic_path });
}
$(document).ready(function() {
alert("test2");
});
</script>
功能删除照片(id,图片路径){
警报(“测试”);
$.post(“ajax.php”,{toid:toid,pic_path:pic_path});
}
$(文档).ready(函数(){
警报(“测试2”);
});
呈现的HTML可能如下所示:
<button id="banButton508" onclick="deletePhoto(508, profile_photos/686733/1_4044.jpg);">BAN 508</button> <img src="../profile/pics/686725/2_3234.jpg" width="150" height="150"/>
ban508
加载页面时会显示“test2”警报,但单击按钮时,该功能不会运行(警报甚至不会显示)
知道为什么吗
编辑:我现在将图片路径用引号括起来,如
<button id="banButton{$id}" class="banButton" onclick="deletePhoto({$id}, "{$picture_path}");">BAN {$id}</button>
BAN{$id}
仍然无法使用此功能。尝试将您的功能与其他功能一起放置在document ready功能中
$(document).ready(function(){
function deletePhoto(id, pic_path) {
alert("test");
$.post("ajax.php", { toid: toid, pic_path: pic_path });
}
});
看起来
picture\u path
是一个字符串文本,用in'
将其括起来,如果id
值也是一个字符串,则也对该字符串执行相同操作
<button id="banButton{$id}" onclick="deletePhoto('{$id}', '{$picture_path}');">BAN {$id}</button>
BAN{$id}
以下代码工作正常
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<button id="banButton508" onclick="deletePhoto(508, 'profile_photos/686733/1_4044.jpg');">BAN 508</button> <img src="../profile/pics/686725/2_3234.jpg" width="150" height="150"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js "></script>
<script type="text/javascript">
function deletePhoto(id, pic_path) {
alert("test"+pic_path);
$.post("ajax.php", { toid: toid, pic_path: pic_path });
}
$(document).ready(function() {
alert("test2");
});
</script>
</body>
试验
禁令508
功能删除照片(id,图片路径){
警报(“测试”+PICU路径);
$.post(“ajax.php”,{toid:toid,pic_path:pic_path});
}
$(文档).ready(函数(){
警报(“测试2”);
});
如果您使用的是jquery,那么为什么不以标准形式操作dom aciton呢 像
控制台中是否有任何错误?也可以共享按钮生成的html吗?可以打开浏览器控制台查看是否有任何错误将图片路径放在单引号中是问题之一。我发现的另一个是由于您建议的控制台(从现在起将一直使用…),deletePhoto传递的是“id”,而不是“toid”。将函数中的“id”参数更改为“toid”。谢谢。谢谢——除此之外,我还必须在deletePhoto函数中将“id”改为“toid”。@DanyP是的。。。。它将给出一个引用错误,因为变量未被引用
<button id="banButton{id}" data-id="{id}" data-image_url="{$picture_path}">BAN {id}</button>
function deletePhoto(id, pic_path) {
alert("test");
$.post("ajax.php", { toid: toid, pic_path: pic_path });
}
$(document).ready(function() {
alert("test2");
$("button[id^='banButton']").click(function(){
var id = $(this).data('id');
var img_url = $(this).data('image_url');
deletePhoto(id, img_url);
});
});