Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript或jquery从foreach检索事件和htmlInput元素_Javascript_Jquery_Ajax_Razor_Foreach - Fatal编程技术网

使用javascript或jquery从foreach检索事件和htmlInput元素

使用javascript或jquery从foreach检索事件和htmlInput元素,javascript,jquery,ajax,razor,foreach,Javascript,Jquery,Ajax,Razor,Foreach,我设法从foreach内部检索了一个动态元素ID,并通过以下方式将其发送给控制器: @using (Html.BeginForm("DeleteConfirmed", "Gifts", FormMethod.Post, new { @class = "form-content", id = "formDiv" })) { foreach (var item in Model.productList) { <input

我设法从foreach内部检索了一个动态元素ID,并通过以下方式将其发送给控制器:

    @using (Html.BeginForm("DeleteConfirmed", "Gifts", FormMethod.Post, new { @class = "form-content", id = "formDiv" }))
    {
        foreach (var item in Model.productList)
        {
            <input type="button" value="Delete" onclick="DeleteButtonClicked(this)" data-assigned-id="@item.ID" />
        }
    }
现在,这很好,因为itemID被正确地检索到了。 由于我想在表单中添加一个@Html.AntiForgeryToken(),我的想法是将MVC控制器的Actionmethod更改为一个JsonResult,在脚本中添加一点Ajax,允许我同时传递itemID和token

比如:

function DeleteButtonClicked(elem) {
   event.preventDefault();
   var form = $('#formDiv');
   var token = $('input[name="__RequestVerificationToken"]', form).val();
   var itemID = $(elem).data('assigned-id');
   if (confirm('sure?')) {
            $.ajax({
                type: 'POST',
                datatype: 'json',
                url: '@Url.Action("DeleteConfirmed", "Gifts")',
                data: {
                   __RequestVerificationToken: token,
                    id: itemID
                },
                cache: false,
                success: function (data) { window.location.href = "/Gifts/UserProfile?userID=" + data; },
                error: function (data) { window.location.href = '@Url.Action("InternalServerError", "Error")'; }
            });
        }
   dynamic }Some 
但是我不知道如何在
中的元素(this=>elem)中添加“event”,我用它来标识foreach循环中的项,以便将其传递给脚本

上面的脚本显然失败了,因为没有“事件”(前提是这将是唯一的错误,我一点也不确定)


需要一些帮助。提前感谢您的时间和考虑。

您要做的是使用jQuery创建事件处理程序:

$('input[type="button"]').on('click', function(event) {
   event.preventDefault();
   var form = $('#formDiv');
   var token = $('input[name="__RequestVerificationToken"]', form).val();
   var itemID = $(this).data('assigned-id');
   if (confirm('sure?')) {
       $.ajax({
           type: 'POST',
           datatype: 'json',
           url: '@Url.Action("DeleteConfirmed", "Gifts")',
           data: {
              __RequestVerificationToken: token,
               id: itemID
           },
           cache: false,
           success: function (data) { window.location.href = "/Gifts/UserProfile?userID=" + data; },
           error: function (data) { window.location.href = '@Url.Action("InternalServerError", "Error")'; }
       });
   }
});

只需确保在渲染按钮后渲染此脚本即可。最好使用
$(document).onReady
技术。

尝试“on”事件处理程序附件()。外部函数是DOM就绪的简写

$(function() {
  $('.some-container').on('click', '.delete-btn', DeleteButtonClicked);
})

谢谢你,伙计,这需要最少的改变,而且效果很好。我对你的回复投了赞成票,但我的声誉得分仍低于15分,这并没有改变帖子的得分。据我所知,$(document).ready()在这种情况下不应被要求为1。脚本开头的$已经暗示了这一点。2.您不能单击尚未渲染的对象。但我很可能错了。
$(function() {
  $('.some-container').on('click', '.delete-btn', DeleteButtonClicked);
})