When I try to use onClick
in the php page like:
<a href="javascript:void(0);" onClick="deleteCourse('<?php echo $row->courseId;?>');" class="delete">Delete</a>
and in js page I used like:
function deleteCourse(course_id){
alert('trying to delete course');
},
It will shows error like
Uncaught ReferenceError: deleteCourse is not defined
at HTMLAnchorElement.onclick but when I use onClick in the same page its working properly.
<?php if($deleteRole == 1){?>
<a href="javascript:void(0);" onClick="deleteCourse('<?php echo $row->courseId;?>');" class="delete">Delete</a>
<?php }?>
function deleteCourse(course_id){
alert('trying to delete course');
}
That user.js is included properly, I have seen it in page source.
<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)
I am changing the attributes. The reason we go for jquery is to seperate HTML and JS codes. Dont use javascript codes inside HTML if you are using jquery.(A suggestion, which you might omit)
<a href="javascript:void(0);" data-courseId="<?php echo $row->courseId;?>" class="delete">Delete</a>
In user.js file (loaded after jquery file)
$(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函数时,无需使用引号
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.