【问题标题】:how can i reset @html.Listbox to empty using jquery in Asp.net MVC如何在 Asp.net MVC 中使用 jquery 将 @html.Listbox 重置为空
【发布时间】:2016-10-03 10:54:45
【问题描述】:

@Html.ListBox("项目类型", new SelectList(db.tblTypes.ToList(), "ID", "Name"), 新 { 多个 = "multiple", id = "projectType" })

这是我希望清空的 ajax 代码

$("#frm").submit(function (e) {

                e.preventDefault(e);

             //$('#frm').submit();

             var e = document.getElementById("myselect");

             var selectedIndices = [];
             $(function () {

                 $.each($('select[multiple]'), function (sIndex, sValue) {
                     if (sIndex == 0) {
                         $.each(this.options, function (oIndex, oValue) {
                             if (this.selected)
                                 selectedIndices.push(oIndex);

                         });
                     } else {
                         for (var i = 2; i < selectedIndices.length; i++) {
                             this.options[selectedIndices[i]].selected = true;

                         }
                     }
                 });
             });

             var project = {

                 ProjectTitle: $('#ProjectTitle').val(),
                 ProjectNumber: $('#ProjectNumber').val(),
                 Portfolio: $('#Portfolio').val(),
                 TotalSalesCC: $('#TotalSalesCC').val(),
                 grossMarginCC: $('#GrossMarginCC').val(),


                 projectMarginCC: $('#ProjectMarginCC').val(),
                 projectStatus: e.options[e.selectedIndex].value,
                 ImpStartDate: $('#ImpStartDate').val(),
                 ImpEndDate: $('#ImpEndDate').val(),
                 projectType: selectedIndices,
                 isSurvey:$('#hide').val()

             };

             if ($("#ProjectNumber").val() == "") {
                 $.growl.error({ message: 'Please fill  project number.' });
                 return false;
             }
             if ($("#ProjectTitle").val() == "") {
                 $.growl.error({ message: 'Please fill project title.' });
                 return false;
             }

             if (selectedIndices.length == 0 || selectedIndices.length < 1) {
                 $.growl.error({ message: 'Please choose atleast one project type.' });
                 return false;
             }


             var s=$("#ImpStartDate").val();
             var e = $("#ImpEndDate").val();
             if (s == "" )
             {
                 $.growl.error({ message: 'Please enter start date.' });
                 return false;
             }
             else if (e == "")
             {
                 $.growl.error({ message: 'Please enter end date.' });
                 return false;
             }

             else if (s > e) {
                 $.growl.error({ message: 'End date should be greater than start date.' });
                 return false;
             }

                 var date = new Date();
                 date.setDate(date.getDate()-1);

                 //$('#ImpStartDate').datepicker({
                 //    startDate: date
                 //});





             $.ajax({
                 type: "POST",
                 url: "/Projects/Create",
                 data: JSON.stringify(project),
                 contentType: "application/json; charset=utf-8",
                 success: function (s) {
                     console.log(s);
                     LoadProjectsGrid();
                     //LoadprojectsGrid();


                     // End For
                     $.growl.notice({ message: ' Your Project created successfuly' });
                     $("#projectType").html('');
                 },
                 failure: function (response) {
                     alert(response.d);
                 }
             });

                 // clear list via dom





                 // or clear list via jquery & dom








             $('#frm').find(".has-success").removeClass("has-success");
             $('#frm').find(".glyphicon-ok").removeClass("glyphicon-ok");
             $('.panel-collapse').collapse('hide');
             $('.panel-title').attr('data-toggle', 'collapse');
             document.getElementById('frm').reset();
             $('#accordion .ui-accordion-content ').hide();








         });

【问题讨论】:

    标签: javascript jquery asp.net-mvc asp.net-mvc-4 asp.net-mvc-5


    【解决方案1】:

    如果您想在客户端清空它,请使用

    $("#projectType").html('');
    

    如果你想在服务器端使用空值

    $("#projectType").val('');
    

    【讨论】:

    • ID 相同,我想在表单提交时清空?
    • 试试$("#projectType").val('');
    • 这不可能,显示完整的Jquery代码,你是怎么调用上面的方法的?
    • $("#frm").submit(function (e) { $("#projectType").val('');)};
    • 我可以像上面那样使用它
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多