简体   繁体   English

onClick在codeigniter中不起作用

[英]onClick is not working in codeigniter

When I try to use onClick in the php page like: 当我尝试在php页面中使用onClick ,例如:

<a href="javascript:void(0);" onClick="deleteCourse('<?php echo $row->courseId;?>');" class="delete">Delete</a>

and in js page I used like: 在js页面中,我使用像:

function deleteCourse(course_id){
    alert('trying to delete course');
 }, 

It will shows error like 它将显示类似的错误

Uncaught ReferenceError: deleteCourse is not defined 未捕获的ReferenceError:未定义deleteCourse

at HTMLAnchorElement.onclick but when I use onClick in the same page its working properly. 在HTMLAnchorElement.onclick上,但是当我在同一页面上使用onClick时,它可以正常工作。

courselist.php courselist.php

<?php if($deleteRole == 1){?>
    <a href="javascript:void(0);" onClick="deleteCourse('<?php echo $row->courseId;?>');" class="delete">Delete</a>
<?php }?>

user.js user.js

function deleteCourse(course_id){
   alert('trying to delete course');
}

That user.js is included properly, I have seen it in page source. 该user.js已正确包含,我已经在页面源代码中看到它。

<a href="javascript:void(0);" onClick="deleteCourse('<?php echo $row->courseId;?>');" class="delete">Delete</a>

The intention of a tag is to redirect. 标签的目的是重定向。 If you are not redirecting, why you want to use a tag, instead i suggest you to button or li tags (if used in menu) 如果您不重定向,为什么要使用标签,我建议您使用按钮或li标签(如果在菜单中使用)

I am changing the attributes. 我正在更改属性。 The reason we go for jquery is to seperate HTML and JS codes. 我们选择jquery的原因是将HTML和JS代码分开。 Dont use javascript codes inside HTML if you are using jquery.(A suggestion, which you might omit) 如果您使用的是jQuery,请不要在HTML内使用JavaScript代码。(一个建议,您可能会省略)

<a href="javascript:void(0);" data-courseId="<?php echo $row->courseId;?>" class="delete">Delete</a>

In user.js file (loaded after jquery file) 在user.js文件中(在jquery文件之后加载)

$(document).on("click", ".delete", function(event){
 event.preventDefault();
 event.stopPropagation();
 courseId = $(this).attr("data-courseId");
 alert('trying to delete course');
//your logic for deleting the course.
});
 <a href="javascript:void(0);" onClick="deleteCourse(<?php echo $row->courseId;?>);" class="delete">Delete</a>

将整数传递给JS函数时,无需使用引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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