首页 > 解决方案 > 我不能在我的代码中使用函数参数

问题描述

我的 JavaScript 代码是

    $(document).ready(function today(tony){
    var mytext = tony;
    $('#tod').click(function(event){

    event.preventDefault();

$.ajax({
    url:"/ajaxdemo",
    data:{text:mytext},
    method:"POST",
    success: function(res){
        alert(res.from)
    },
    error: function(err){
        console.log(err)
    }

})

}) })

我的html代码是

<a id="tod" href="" onclick="today(tony)" ><i class="fa fa-calendar"></i>{{this.tod}}</a>

我不能接受名为 today(tony) 的函数中的参数

标签: javascriptfunctionarguments

解决方案


我得到了解决方案

将javascript代码更改为

function today(tod) {
$.ajax({
    url: "/ajaxdemo",
    data: {
        text: tod
    },
    method: "POST",
    success: function (res) {
        alert(res.from)
    },
    error: function (err) {
        console.log(err)
    }

});
return false;
}

将 HTML 代码更改为

<a id="tod" href="" onclick="today(tony)" ><i class="fa fa-calendar"></i>{{this.tod}}</a>

推荐阅读