亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 JavaScript 函數鏈接到 HTML 錨標記

如何將 JavaScript 函數鏈接到 HTML 錨標記

C#
蝴蝶不菲 2022-07-10 16:18:26
如何將此 JavaScript 代碼鏈接到 HTML 的錨標記:<script type="text/javascript">    $(document).on('click', 'a', function () {        $.ajax({            type: 'POST',            url: '@Url.Action("/brandsOfACategory")',            contentType: 'application/json; charset:utf-8',            data: JSON.stringify({ id: this.id })        })    });錨標簽:<a id="@c.Key" href ="???" onclick="???">@c.Key</a>brandsOfACategory動作方法:[HttpPost]    public ActionResult brandsOfACategory(string id)    {        var result = db.Items.Where(x => x.Category.Name.Equals(id)).Select(x => x.BrandID).ToList();        var ListOfBrands = db.Brands.Where(t => result.Contains(t.BrandID)).ToList();        return View(ListOfBrands);    }brandsOfACategory.cshtml是:@model IEnumerable<OnlineStore.Models.Brand>@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Brands in a Category</title></head><body>    @foreach (var i in Model)    {        @i.Name.ToString();    }</body></html>
查看完整描述

2 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

你可以這樣寫錨標簽-


<a id="@c.Key" href ="javascript:void(0);" onclick="postBrands(@c.Key)">@c.Key</a> //replace postBrands with desired function name

然后在 javascript 中定義將包含發布請求的函數-


function postBrands(key) {

        $.ajax({

            type: 'POST',

            url: '@Url.Action("/brandsOfACategory")',

            contentType: 'application/json; charset:utf-8',

            data: JSON.stringify({ id: key })

        })

}


查看完整回答
反對 回復 2022-07-10
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

您可以輸入 href 然后href在客戶端點擊代碼中獲?。?/p>


<a id="@c.Key" href ="@Url.Action("actionName","controllerName")">@c.Key</a>

在點擊事件中,您可以編寫以下內容:


$(document).on('click', 'a', function () {

    var Url = $(this).attr("href"); // get href value

    $.ajax({

        type: 'POST',

        url: Url, // use it here

        contentType: 'application/json; charset:utf-8',

        data: JSON.stringify({ id: this.id })

    })


查看完整回答
反對 回復 2022-07-10
  • 2 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號