dpzr52746
2017-05-22 06:51
浏览 20
已采纳

onClick不能在codeigniter中工作

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.

courselist.php

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

user.js

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

That user.js is included properly, I have seen it in page source.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duannaikuang1301 2017-05-22 07:46
    已采纳
    <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.
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dougu1896 2017-05-22 07:02
     <a href="javascript:void(0);" onClick="deleteCourse(<?php echo $row->courseId;?>);" class="delete">Delete</a>
    

    You dont need to use quotes while passing integer to JS function

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题