Jquery 按钮未禁用,文本值为';单击后更改nt

Jquery 按钮未禁用,文本值为';单击后更改nt,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我基本上有一个表,有一个点击应用按钮功能的工作板。我编写了一些ajax函数代码和jquery,这样每当用户单击按钮时,按钮就会被禁用,文本也会发生变化。现在ajax按钮确实起作用了,单击后数据库中有一个更改,但我无法禁用该按钮并更改文本。请帮忙 以下是Ajax链接按钮的外观: @if (User.Identity.IsAuthenticated) { @Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.Pos

我基本上有一个表,有一个点击应用按钮功能的工作板。我编写了一些ajax函数代码和jquery,这样每当用户单击按钮时,按钮就会被禁用,文本也会发生变化。现在ajax按钮确实起作用了,单击后数据库中有一个更改,但我无法禁用该按钮并更改文本。请帮忙

以下是Ajax链接按钮的外观:

  @if (User.Identity.IsAuthenticated)
{
 @Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.PositionID }, new AjaxOptions
 {
       HttpMethod = "POST",
       Confirm = "Are you sure you want to apply for " + item.Title + "?",
       OnSuccess = "deactive"
  }, new { @class = "btn btn-info float-right", id = "oneClick" + item.PositionID, style = "margin-left:10px; margin-right:10px" })
  }
以下是jquery部分:

  <script type="text/javascript">
    function deactive(data) {
        $('#oneClick' + data.id)addClass('disabled').text("Applied");

    }
</script>
整个cshtml页面

    @model PagedList.IPagedList<JobBoard.DATA.EF.Position>
         @using System;
         @using PagedList.Mvc
       @{
        ViewBag.Title = "Index";
        }
        <section class="section-hero overlay inner-page bg-image" 
         style="background-image: url('/Content/images/hero_1.jpg');" 
         id="home-section">
         <div class="container">
        <div class="row">
            <div class="col-md-7">
                   <h1 class="text-white font-weight-bold">Admin Panel</h1>
                    <div class="custom-breadcrumbs">
                         <a href="/Home/Index">Home</a> <span class="mx-2 slash">/</span>
                         <a href="/Home/Positions">Positions</a> <span class="mx-2 slash">/</span>
                    <span class="text-white"><strong>@ViewBag.Title</strong></span>
                </div>
            </div>
        </div>
    </div>
</section>
           <section class="site-section" id="next-section">
           <div class="container">
             <div class="row">
               <div class="col-lg-12 mb-5 mb-lg-0">


                   <p>
                       @Html.ActionLink("Create New", "Create")
                   </p>

                   @using (Html.BeginForm("Index", "Positions", FormMethod.Get))
                {
                    <div style="border:2px solid lightblue; margin-top:10px;margin-bottom:10px;padding:15px">
                        Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
                        <input type="submit" value="Search" />
                    </div>

                }
                <span style="margin-top:10px; margin-bottom:10px;">
                    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page, currentFilter = ViewBag.CurrentFilter }))
                </span>
                <a href="#">content</a>
                @if (Model.Count() > 0)
                {
                    <table id="myTable" class="table table-striped">
                        <thead>
                            <tr>
                                <th>
                                    Title
                                </th>
                                <th>
                                    Job Description
                                </th>
                                <th></th>
                            </tr>
                        </thead>

                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    <td>
                                        <a href="/Positions/Details/@item.PositionID"> @Html.DisplayFor(modelItem => item.Title)</a>
                                    </td>
                                    <td style="width:270px">

                                        @*@item.JobDescription.Substring(0, Math.Min(item.JobDescription.Length, 200)) ...*@



                                        @Html.Raw(item.JobDescription.Substring(0, Math.Min(item.JobDescription.Length, 200))) ...


                                    </td>
                                    <td>
                                        @if (User.IsInRole("Admin") || User.IsInRole("Manager"))
                                        {
                                            <span class="float-right" style="margin-left:10px; margin-right:10px">
                                                @Html.ActionLink("Edit", "Edit", new { id = item.PositionID }, new { @class = "btn btn-primary" })
                                            </span>
                                            <span class="float-right" style="margin-left:10px; margin-right:10px"> @Html.ActionLink("Delete", "Delete", new { id = item.PositionID }, new { @class = "btn btn-danger" })</span>

                                        }
                                        <span class="float-right" style="margin-left:10px; margin-right:10px"> @Html.ActionLink("Details", "Details", new { id = item.PositionID }, new { @class = "btn btn-warning " })</span>

                                        @if (User.Identity.IsAuthenticated)
                                        {
                                            @Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.PositionID }, new AjaxOptions
                                       {
                                           HttpMethod = "POST",
                                           Confirm = "Are you sure you want to apply for " + item.Title + "?",
                                           OnSuccess = "deactive"
                                       }, new { @class = "btn btn-info float-right", id = "oneClick" + item.PositionID, style = "margin-left:10px; margin-right:10px" })
                                        }




                                    </td>
                                </tr>
                            }
                        </tbody>

                    </table>
                }
                else
                {
                    <p>Sorry your criteria did not return any result.Please try again or or hit "Go!" to refresh the page</p>
                }

            </div>
        </div>
    </div>
</section>
          @section scripts{

    <script type="text/javascript">
        function deactive(data) {
            $('#oneClick' + data.id).addClass('disabled').text("Applied");

        }
    </script>


    }
@model PagedList.IPagedList
@使用制度;
@使用PagedList.Mvc
@{
ViewBag.Title=“Index”;
}
管理面板
/
/
@ViewBag.Title

@ActionLink(“新建”、“创建”)

@使用(Html.BeginForm(“Index”,“Positions”,FormMethod.Get)) { 按名称查找:@Html.TextBox(“SearchString”,ViewBag.CurrentFilter为字符串) } @Model.PageCount的@页(Model.PageCountUrl.Action(“Index”,new{page,currentFilter=ViewBag.currentFilter})) @如果(Model.Count()>0) { 标题 职位描述 @foreach(模型中的var项目) { @*@item.JobDescription.Substring(0,Math.Min(item.JobDescription.Length,200))*@ @Raw(item.JobDescription.Substring(0,Math.Min(item.JobDescription.Length,200))。。。 @if(User.IsInRole(“Admin”)| User.IsInRole(“Manager”)) { @ActionLink(“编辑”,“编辑”,新的{id=item.PositionID},新的{@class=“btn btn primary”}) @ActionLink(“删除”,“删除”,新建{id=item.PositionID},新建{@class=“btn btn danger”}) } @ActionLink(“详细信息”,“详细信息”,新的{id=item.PositionID},新的{@class=“btn btn warning”}) @if(User.Identity.IsAuthenticated) { @ActionLink(“1-Click-Apply”,“OneClickApply”,new{id=item.PositionID},new AjaxOptions { HttpMethod=“POST”, 确认=“您确定要申请“+项目.标题+”?”, OnSuccess=“deactive” },新的{@class=“btn btn info float right”,id=“oneClick”+item.PositionID,style=“左边距:10px;右边距:10px”}) } } } 其他的 { 很抱歉,您的条件没有返回任何结果。请重试或点击“Go!”刷新页面

} @节脚本{ 功能停用(数据){ $('#oneClick'+data.id).addClass('disabled').text(“applicated”); } }
在ajax
OnSuccess
中,您没有传递函数
deactivate
的位置id,这就是为什么您的脚本没有执行任何操作,因为函数参数
数据
为空

@Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.PositionID }, new AjaxOptions
 {
       HttpMethod = "POST",
       Confirm = "Are you sure you want to apply for " + item.Title + "?",
       OnSuccess = "deactive"
  }, new { @class = "btn btn-info float-right", id = "oneClick" + item.PositionID, style = "margin-left:10px; margin-right:10px" })
  }
因此,要更正此问题,您应该使用
PositionId
参数调用
deactive()

OnSuccess = "deactive('" + item.PositionID + "')"
然后在脚本中,您可以通过执行以下操作来访问id

function deactive(id) {
        $('#oneClick' + id)addClass('disabled').text("Applied");

    }
您的Ajax助手应该如下所示

@Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.PositionID }, new AjaxOptions
 {
       HttpMethod = "POST",
       Confirm = "Are you sure you want to apply for " + item.Title + "?",
       OnSuccess = "deactive('" + item.PositionID + "')"
  }, new { @class = "btn btn-info float-right", id = "oneClick" + item.PositionID, style = "margin-left:10px; margin-right:10px" })
  }

你还可以发布操作代码吗?@Keith刚刚编辑了这篇文章
@Ajax.ActionLink("1-Click-Apply", "OneClickApply", new { id = item.PositionID }, new AjaxOptions
 {
       HttpMethod = "POST",
       Confirm = "Are you sure you want to apply for " + item.Title + "?",
       OnSuccess = "deactive('" + item.PositionID + "')"
  }, new { @class = "btn btn-info float-right", id = "oneClick" + item.PositionID, style = "margin-left:10px; margin-right:10px" })
  }