2015-10-15 51 views
0

後dsiplay警報消息其實我保存數據,所以請大家幫我出如何使用保存數據的Jquery

我下面的代碼之前得到警報信息顯示是有

 for (var i = 0; i < rows_count; i++) { 
       var count = 0; 
       $.ajax({ 
        type: "POST", url: "Default.aspx/update_extraHoursWorked", cache: false, data: "{'empID':'" + $("#emp_id" + i).text() + "','emp_code': '" + $("#emp_code" + i).text() + "','emp_category':'" + $("#emp_category" + i).text() + "','g1': '" + $("#txtgen_Three" + i).val() + "','f1': '" + $("#txtfirst_Three" + i).val() + "','s1': '" + $("#txtsecond_Three" + i).val() + "','t1': '" + $("#txtthrid_Three" + i).val() + "','g2': '" + $("#txtgen_Four" + i).val() + "','f2': '" + $("#txtfirst_Four" + i).val() + "','s2': '" + $("#txtsecond_Four" + i).val() + "','t2': '" + $("#txtthrid_Four" + i).val() + "','g3': '" + $("#txtgen_Five" + i).val() + "','f3': '" + $("#txtfirst_Five" + i).val() + "','s3': '" + $("#txtsecond_Five" + i).val() + "','t3': '" + $("#txtthrid_Five" + i).val() + "','contracortName': '" + $('#ddlContractorNames>option:selected').text() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", 
        success: function (data) { }, error: function (e) { alert('Error, Update att.'); } 

       }); 
      } enableLastTwoDays(true); 

       alert('Saved Successfully'); 


     } 
+0

化妝警報:功能(數據){ – ArK

+0

你想超過警戒其他成功的塊裏面做還有什麼? –

+0

嗨,其實我需要警告消息將完成rowcount後顯示,如果我們把每一次警報信息將成功塊內。謝謝你。 – durga

回答

0

試試這個,警報應在成功函數,如果ajax請求成功則調用該函數。您也可以在這個成功功能中從服務器獲取響應數據。如果數據是從服務器返回的,請嘗試console.log(data)以在瀏覽器控制檯中顯示數據。

 for (var i = 0; i < rows_count; i++) { 
       var count = 0; 
       $.ajax({ 
        type: "POST", url: "Default.aspx/update_extraHoursWorked", cache: false, data: "{'empID':'" + $("#emp_id" + i).text() + "','emp_code': '" + $("#emp_code" + i).text() + "','emp_category':'" + $("#emp_category" + i).text() + "','g1': '" + $("#txtgen_Three" + i).val() + "','f1': '" + $("#txtfirst_Three" + i).val() + "','s1': '" + $("#txtsecond_Three" + i).val() + "','t1': '" + $("#txtthrid_Three" + i).val() + "','g2': '" + $("#txtgen_Four" + i).val() + "','f2': '" + $("#txtfirst_Four" + i).val() + "','s2': '" + $("#txtsecond_Four" + i).val() + "','t2': '" + $("#txtthrid_Four" + i).val() + "','g3': '" + $("#txtgen_Five" + i).val() + "','f3': '" + $("#txtfirst_Five" + i).val() + "','s3': '" + $("#txtsecond_Five" + i).val() + "','t3': '" + $("#txtthrid_Five" + i).val() + "','contracortName': '" + $('#ddlContractorNames>option:selected').text() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", 
        success: function (data) { alert('Saved Successfully'); }, error: function (e) { alert('Error, Update att.'); } 

       }); 
      } enableLastTwoDays(true); 




     } 

修改後的代碼如下,下面的代碼是給U上的一個想法,這個理想代碼不應該在你的生產現場這樣使用。在這裏,對於每一個成功保存的行,調用保存函數,其中inturn增加了count_successful的計數。循環結束後,如果它等於行數

var count_successful = 0; 
     for (var i = 0; i < rows_count; i++) { 
       var count = 0; 
       $.ajax({ 
        type: "POST", url: "Default.aspx/update_extraHoursWorked", cache: false, data: "{'empID':'" + $("#emp_id" + i).text() + "','emp_code': '" + $("#emp_code" + i).text() + "','emp_category':'" + $("#emp_category" + i).text() + "','g1': '" + $("#txtgen_Three" + i).val() + "','f1': '" + $("#txtfirst_Three" + i).val() + "','s1': '" + $("#txtsecond_Three" + i).val() + "','t1': '" + $("#txtthrid_Three" + i).val() + "','g2': '" + $("#txtgen_Four" + i).val() + "','f2': '" + $("#txtfirst_Four" + i).val() + "','s2': '" + $("#txtsecond_Four" + i).val() + "','t2': '" + $("#txtthrid_Four" + i).val() + "','g3': '" + $("#txtgen_Five" + i).val() + "','f3': '" + $("#txtfirst_Five" + i).val() + "','s3': '" + $("#txtsecond_Five" + i).val() + "','t3': '" + $("#txtthrid_Five" + i).val() + "','contracortName': '" + $('#ddlContractorNames>option:selected').text() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", 
        success: function (data) { count_successful = count_successful+1; }, error: function (e) { alert('Error, Update att.'); } 

       }); 
      } enableLastTwoDays(true); 

     } 

if(count_successful == rows_count){ 
    alert('Saved all rows successfully'); 
} 
+0

謝謝你,但它顯示每一行的警報消息,但我需要在所有行完成後提醒消息 – durga

+0

@durga每個成功的ajax請求都會觸發成功功能並調整警報,如果你希望警報完全顯示行,檢查後調用警報,並確保所有的Ajax請求都是成功的。我會修改我的上面的代碼給你的概念 – diabolicfreak

+0

你不能。由於ajax是一個異步函數,因此您將在successCount外部成功塊中獲取未定義的值。考慮閱讀本文http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call –

0

count_successful計數檢查你可以指望取得圓滿成功,並最終警報..

count_success= 0; 
for (var i = 0; i < rows_count; i++) { 
        //--- 
        //-- 
         success: function (data) { count_success++; }, 
         error: function (e) { alert('Error, Update att.'); } 

        }); 
       } enableLastTwoDays(true); 

        alert(count_success +'Record Saved Successfully'); 


      } 
+0

嗨,謝謝你分享你的答案,但count_success不增加,請幫助我 – durga

0

有一個隱藏的領域各自對這個功能並在每次成功時增加/改變它的值。最後,如果successCount值等於總行數,則顯示警報。

function someFunction() { 

     for (var i = 0; i < rows_count; i++) { 
      var count = 0; 
      $.ajax({ 
       type : "POST", 
       url : "Default.aspx/update_extraHoursWorked", 
       cache : false, 
       data : "{}", 
       contentType : "application/json; charset=utf-8", 
       dataType : "json", 
       success : function(data) { 
       $('#successCountID').val(i); // changed i+1 to i 
       }, 
       error : function(e) { 
        alert("Error, Update att."); 
       } 

      }); 
     } 
     enableLastTwoDays(true); 
    $(document).ajaxStop(function() { 
     // place code to be executed on completion of last outstanding ajax call here 
     var count = $('#successCountID').val(); 
     if (count == rows_count) { 
      alert("Saved Successfully"); 
      $('#successCountID').val(0); 
     } 
    }); 
    } 

添加一個隱藏字段的形式內成功

<input type="hidden" name="successCount" id="successCountID" value="0"> 
+1

你不能。由於ajax是一個異步函數,因此您將在successCount外部成功塊中獲取未定義的值。考慮閱讀本文http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call –

+0

請參閱我的編輯帖子。感謝有價值的信息@Ghazali。 –

+0

歡迎隊友,你有沒有試過在你的環境中使用這個解決方案? –