如何开始点击在 C# 服务器端代码中使用 html 工作

How to get onClick to work using html within C# server side code

提问人:Zidane 提问时间:3/4/2022 更新时间:3/4/2022 访问量:523

问:

我正在尝试让我的onClick工作,但是当我单击时,什么也没发生,也没有错误来深入了解问题可能是什么

var data = new FormData();
            data.append("cart_list", new_cart);
            $.ajax({
            url: "@Url.Action("GetCart", "Home")",
                type: "POST",
            processData: false,
            contentType: false,
            data: data,
            success: function (result) {
                    //code after success
                    var html = "";
               // var removeIndex
                for (index = 0; index < result.result.length; ++index) {
                         removeIndex = result.result[index]; 
                        html = html + '<li class="minicart-product">' + result.result[index].HFR +;
                      
                        Html = Html + ' <a class="product-item_remove" onclick="RemoveFromCart()"></a>';
                        html = html + '<i class="ion-android-close"></i>';
                        html = html + '</a>';
                        html = html + '<div class="product-item_img">';
                        html = html + '<img src="../Content/assests/images/Catalogue/' + result.result[index].Foto + '" alt="../Content/assests/images/Catalogue/default.png">';
                        html = html + '</div>';
                        html = html + '<div class="product-item_content">';
                        html = html + '<a class="product-item_title" href="">' + result.result[index].TractorModel + " " + result.result[index].Description + '</a>';
                        html = html + '<span class="product-item_quantity">' + result.result[index].HFR + '</span>';
                        html = html + '</div>';
                        html = html + '</li>';
                    }


                $("#myCart").html(html);
                },
            error: function (er) {

                    alert(er.responseText);
                }

            });

        }

        function RemoveFromCart()
        {
            alert("Remove me")
        }
JavaScript C# HTML ASP.NET-MVC 服务器端

评论

0赞 Tiny Wang 3/4/2022
如何添加 OnClick 事件?恐怕问题是由于添加 onclick 事件不正确或您没有正确加载 js 代码造成的。
0赞 Tiny Wang 3/4/2022
您也可以尝试您的脚本。我不确定您是否有这些问题,因为我真的不了解您的实际要求。@section scripts{}

答:

0赞 Tiny Wang 3/4/2022 #1

您可以检查 onclick 事件是否被正确触发。例如,我有一个 asp.net 核心 MVC 项目,它默认集成了 Jquery,在我的代码片段中,我有一个子句用于测试它是否被触发。代码段位于默认主页中。alert

    @{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<input id="picContent" type="file" name="photo" value="please upload a picture" />
<input id="btn1" type="button" value="upload"/>

@*@section scripts
{*@
<script>
    $("#btn1").click(function(){
        alert(1);
        return;
        var formData = new FormData();
        var img = $("#picContent")[0].files[0];
        console.log(img);
        console.log($("#picContent").val());
        formData.append("file", img);
        $.ajax({
            data: formData,
            url:"https://localhost:44340/home/upload",
            type:"post",
            data:formData,
            processData: false,
            contentType: false,
            success:function(data){
                alert(data);
            }
        })        
    })
</script>
@*}*@
0赞 Amit 3/4/2022 #2

它是 html 而不是 Html。

替换此行:

 Html = Html + ' <a class="product-item_remove" onclick="RemoveFromCart()"></a>';

 html = html + ' <a class="product-item_remove" onclick="RemoveFromCart()"></a>';