2014-09-04 34 views
0

我有一個網格,其中每個行都添加了複選框。當我點擊刪除按鈕而不點擊複選框時,它會顯示「請至少選擇一行來執行」。點擊複選框時也會顯示相同的信息。請幫助我找出解決方案和我在下面提到的代碼中所犯的錯誤。刪除函數點擊jquery中的複選框傳遞ID

 //Calling Delete function clicking on checkbox 
       $(document).ready(function() { 
    //Grid Function 
    $.ajax(
           { 
            url: "GetCompanyTypeMasterDetails", 
            type: "get", 
            dataType: "json", 
            contentType: "application/json", 
            success: function (data) { 
             $("#grid").igGrid({ 
              autoGenerateColumns: false, 
              primaryKey: "CompanyTypeID", 
              columns: [ 

               { headerText: "Company Type ID", key: "CompanyTypeID", dataType: "Numeric", hidden: true }, 
               { 
                headerText: "", key: "CompanyTypeID", dataType: "Numeric", width: "80", 
                template: '<table><tr><td><input type="image" src="/Images/GridImages/edit.png" onclick = "EditRecords(${CompanyTypeID})" /></td><td><input type="image" src="/Images/GridImages/delete.png" onclick = "DeleteRecords(${CompanyTypeID})" /></td></tr></table>' 
               }, 
               { headerText: "", key:"CompanyTypeID", dataType:"Numeric", width:"50", template: '<input type="checkbox" id="chkID" />'}, 
               { headerText: "Company Type", key: "CompanyTypeName", dataType: "string", width: "600" }, 
               { headerText: "Company Code", key: "Code", dataType: "string", width: "600" }, 


              dataSource: data} 
}); 
       $("#btnDelete").click(function (ID) { 
           var isSelected = $('#chkID').is(':checked'); 
           if (!isSelected) { 
            alert("Please select atleast one row to perform this action"); 
           } 
           else if (isSelected) { 
            DeleteRecords(ID); 
           } 
          }); 
         }); 
       }); 

      //Delete Function 
         function DeleteRecords(ID) { 
          $.ajax(
            { 
             url: "DeleteRecords", 
             type: "post", 
             dataType: "json", 
             data: {CompanyTypeID : ID}, 
             success: function (data) { 
              alert("Are you sure you want to Delete?"); 
              location.reload(); 
             }, 
             error: function (status) { 
              alert(status.responseText); 
             } 
            }); 
         } 
+1

您正在使用相同的ID爲每個刪除按鈕,複選框,使用不同的ID或使用類。 – 2014-09-04 06:17:38

+0

向我們展示瞭如何將行添加到網格。 – 2014-09-04 06:18:21

+1

你想要一個確切的問題的解決方案? – Leo 2014-09-04 06:28:51

回答

0

$('#chkID')引用id = chkID的元素。我認爲你在這裏的問題是你有多個相同的ID複選框。

可能的解決辦法是:將所有的複選框類(可以說「chkBox」):

$("#btnDelete").click(function (ID) { 
    if ($('.chkBox:checked').length == 0) { 
     alert("Please select atleast one row to perform this action"); 
    } 
    else { 
     DeleteRecords(ID); 
    } 
}); 

這裏工作Fiddle

+0

向下投票! - 請解釋 – 2014-09-04 06:21:15

+0

他要求發現一個錯誤,這是一個錯誤! – 2014-09-04 06:22:41

+0

你是對的。這個問題非常模糊,我假設他在勾選複選框時不想顯示消息。刪除donwvote ... – Leo 2014-09-04 06:26:59

相關問題