2017-04-05 109 views
0

当警告框打开时,它会自动关闭现有的打开的对话框。我想确保显示错误消息时自动关闭对话框。我尝试使用返回false但不起作用 不知道我在哪里出错。对话框自己关闭

<div id="dialog-form" title="Create new category"> 
    <p class="validateTips">All form fields are required.</p> 
    <form> 
     <fieldset> 
      <label for="code">Code</label> 
      <input type="text" name="code" maxlength="9" id="code" value="" class="text ui-widget-content ui-corner-all"/> 
      <label for="name">Year</label> 
      <input type="text" name="name" id="name" maxlength="4" value="" 
        class="text ui-widget-content ui-corner-all" /> 
     </fieldset> 
    </form> 
</div> 

$(function() {  
    var new_dialog = function (type, row) { 
     var dlg = $("#dialog-form").clone(); 
     var year = dlg.find(("#acad_year")), 
     code = dlg.find(("#acad_code")); 
     type = type || 'Create'; 
     var config = { 
      autoOpen: true, 
      height: 300, 
      width: 350, 
      modal: true, 
      buttons: { 
       "Create Academic Year": function() { 
        save_data("create"); 
       }, 
       "Cancel": function() { 
        dlg.dialog("close"); 
       } 
      }, 
      close: function() { 
       dlg.remove(); 
      } 
     };   

     function save_data(strings) { 
      var id; 
      dlg.dialog("close"); 
      if (strings == 'edit') { 
       id = $(row.children().get(0)).text(); 
      } else { 
       id = "NU=L"; 
      } 
      $.ajax({ 
       url: 'acad_year.php', 
       type: 'post', 
       dataType: 'json', 
       data: { 
        id: id, 
        from: strings, 
        acad_year: year.val(), 
        acad_code: code.val() 
       }, 
       success: function (response) { 
        if (response[0] === "REDUNDANT") { 
         alert("Data already exists. "); 
        }else if(response[0] === "EMPTY"){ 

         alert("Data can't be left empty "); 
        } else { 
         sortTable(1); 
         $("#acodes tbody").append("<tr>" + "<td style='display:none;'>" + response[1] + "<td>" + year.val() + "</td>" + "<td>" + code.val() + "</td>" + "<td><a href='' class='edit'>Edit</a></td>" + "<td><span id='" + response[1] + "' class='delete'><a href=''>Delete</a></span></td>" + "</tr>");  
        } 
       }, 
       error: function (error) { 
        alert("ERROR.." + error[0]); 
       } 
      }); 

     } 
    }; 

回答

0

而不是提醒错误,你可以写错误信息。 例子:

<div id="a"> 
    <div id="Error-msg" style="display:none;"></div> 
    ...Some Form .... 
    </div> 
</div> 

<script> 

    if(error){ 
    $("#Error-msg").append("<p>You have an error</p>"); 
    } 

</script> 

而不是

error: function (error) { 
alert("ERROR.." + error[0]); 
} 

您可以使用

error: function (error) { 
$("#Error-msg").append("ERROR.." + error[0]); 
}