使用Jquery保存数据后如何使用dsiplay警告消息

时间:2015-10-15 05:53:29

标签: javascript 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');


        }

3 个答案:

答案 0 :(得分:0)

试试这个,警报应该在success函数中,如果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);




        }

修改后的代码如下,下面的代码是给你一个想法,理想情况下,这个代码不应该在你的生产网站中使用。对于成功保存的每一行,都会调用save函数,其中inturn会增加count_successful的计数。循环完成后,检查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');
 }

答案 1 :(得分:0)

你可以算上每一次成功并最终提醒......

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');


            }

答案 2 :(得分: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">