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

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

“foreach”中的 <button>

“foreach”中的 <button>

泛舟湖上清波郎朗 2023-10-20 15:00:27
我有兩個問題。我正在使用 ado .NET Core。我想顯示一系列帶有酒名的按鈕。當用戶單擊 Javascript 函數時 - 它會顯示葡萄酒的歷史記錄。第一個問題:Javascript 函數工作正常,但僅適用于列表的第一個按鈕。第二個問題:函數返回給我的 JSON 對象的第二個元素一開始必須包含“display: none”。這是代碼:(謝謝)<ul>                    @foreach(var item in ViewBag.Vini)                    {                         <li class="col-lg-12 col-sm">                                <input type="hidden" class="tipoVino" value="@item.TipoVino"/>                                <input type="hidden" class="nomeVino" value="@item.NomeVino"/>                                <button type="button" class="btnPost" value="@item.NomeVino">@item.NomeVino</button>                        </li>                    }                </ul>$(document).ready(function () {    $('.btnPost').on('click', function () {        document.getElementById("text1").style.display = "none";        document.getElementById("text1").style.display = "block";        var item1 = $('.nomeVino').val();        var item2 = $('.tipoVino').val();                $.ajax({            type: "POST",            url: "?handler=Send",            beforeSend: function (xhr) {                xhr.setRequestHeader("XSRF-TOKEN",                    $('input:hidden[name="__RequestVerificationToken"]').val());            },            data: JSON.stringify({                Item1: item1,                Item2: item2            }),            contentType: "application/json; charset=utf-8",            dataType: "json",            success: function (response) {                var dvItems = $("#text");                dvItems.empty();                $.each(response, function (i, item) {                    var $tr = $('<p id=par' + i + '>').append(item).appendTo(dvItems);                 });            },            failure: function (response) {                alert(response);            }        });    })});
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

對于你原來的問題,我看到你已經編輯了它,因為你已經從 ids 更改為類,正如 Emiel Zuurbier 所評論的那樣。


您仍然遇到的問題在這里:


var item1 = $('.nomeVino').val();

var item2 = $('.tipoVino').val();

這將始終檢索 DOM 中找到的類“nomeVino”和“tipoVino”的第一個項目的值。在 ajax 請求中向后端發送數據時,您正在使用變量 item1 和 item2,因此無論按哪個按鈕,您都將始終發送相同的值。


由于您已經在使用 jQuery,我建議使用數據屬性。以下是使用 data- 屬性的 HTML 的示例:


<ul>

     @foreach(var item in ViewBag.Vini)

     { 

           <li class="col-lg-12 col-sm">

               <button type="button" class="btnPost" data-tipo-vino="@item.TipoVino" data-nome-vino="@item.NomeVino" value="@item.NomeVino">@item.NomeVino</button>

           </li>

      }

 </ul>

你的 js 看起來像這樣:


$(document).ready(function () {


$('.btnPost').on('click', function (event) {

    document.getElementById("text1").style.display = "none";

    document.getElementById("text1").style.display = "block";

    var item1 = $(event.target).data('nome-vino');

    var item2 = $(event.target).data('tipo-vino');

    

    $.ajax({

        type: "POST",

        url: "?handler=Send",

        beforeSend: function (xhr) {

            xhr.setRequestHeader("XSRF-TOKEN",

                $('input:hidden[name="__RequestVerificationToken"]').val());

        },

        data: JSON.stringify({

            Item1: item1,

            Item2: item2

        }),

        contentType: "application/json; charset=utf-8",

        dataType: "json",

        success: function (response) {

            var dvItems = $("#text");

            dvItems.empty();

            $.each(response, function (i, item) {

                var $tr = $('<p id=par' + i + '>').append(item).appendTo(dvItems); 

            });

        },

        failure: function (response) {

            alert(response);

        }

    });

})});


查看完整回答
反對 回復 2023-10-20
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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