javascript onclick函数不适用于参数

时间:2015-05-01 12:01:15

标签: javascript php html

如果我尝试将参数传递给onclick事件中的函数,它不起作用,clase将从codeigniter控制器访问clase / index方法。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- Bootstrap -->
        <link href="<?php echo base_url('bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="<?php echo base_url('bootstrap/js/bootstrap.min.js');?>"></script>
        <script language='javascript' type='text/javascript' src="<?php echo base_url('js/action.js');?>"></script>

    </head>
    <body>   
        <div class='container'>
            <h1>Esta página es solo accesible para el administrador.</h1>
             <a class="btn btn-success" onclick ="go('clase')">Volver</a>'
        </div>
    </body>
</html>

这就是功能

function go(url){
    //var url = "clase";    
    $(location).attr('href',url);
}

没有参数它可以工作。但如果我传递参数,则没有任何反应。

2 个答案:

答案 0 :(得分:2)

这对我有用:

<script type="text/javascript">
function go(url){  
    $(location).attr('href',url);
}
</script>

<a href="javascript:void(0);" class="btn btn-success" onclick="go('clase')">Volver</a>

答案 1 :(得分:2)

从您的锚标记中删除最后comma,并将href添加到锚标记

<a href="#" class="btn btn-success" onclick ="go('clase')">Volver</a>

并确保go()script标记