JS在两次更新确认框上更改了MVC

时间:2017-10-26 09:06:17

标签: javascript model-view-controller drop-down-menu onchange

我有一个问题,在Telerik下拉列表中我有一个函数onchange选择。如果选择了列表中的项,则此函数使用ajax调用保存值。如果选择不正常将显示确认框,如果用户按OK将在另一页上重定向。如果按取消需要填充空字符串的字段 问题是:如果用户按下CANCEL按钮,则确认框会触发两次......我试图在$('#Assessment')之后返回false。val('');但不起作用.. 有什么解决方案吗?

DROPDOWNLIST:

@(Html.Kendo().DropDownListFor(m => m)
     .Name("Assessment").HtmlAttributes(new { @style = "text-align:center", onchange = "saveAssessment()" })
        .OptionLabel("Select an assessment")
        .DataTextField("DisplayName")
        .DataValueField("Value")
        .Value("DisplayName")

JS功能

function saveAssessment() {
     var assessmentId = $('#Assessment').val();
     var grid = $('#_gridProjectCheckList').data('kendoGrid');
     var selectedItem = grid.dataItem(grid.select());
     var ciId = selectedItem.Id;
     var ciText = $("#Assessment").data("kendoDropDownList").text();
     if (assessmentId !== null && (ciText === 'Ok' || ciText === 'NotApplicable')) {
         $.ajaxSetup({ cache: false });
         $.ajax({
             url: 'UpdateProjectCheckItemAssessment',
             type: "POST",
             data: { assessmentId: assessmentId, id: ciId },
             success: function (response) {
                 $('#Assessment').val(response.assessmentVal);
                 $('#_gridProjectCheckList').data('kendoGrid').dataSource.read();
             }
         });
     }
     else {
         var msgRedir = confirm("You will be redirected to Assessment Page");
         if (msgRedir) {
             window.location.href = 'UpdateProjectCheckItem' + '/' + selectedItem.Id;
         }
             $('#Assessment').val('');
     }
 }

0 个答案:

没有答案