2015-12-21 97 views
1

我正在写一些JavaScript和HTML,我想为用户错误处理(不填写字段)做自定义对话框。出于某种原因,当出现用户错误时,这些框不显示。我的Java纸条和HTML如下:自定义对话框不显示

 <script>$(document).ready(function(){ 
       $('#submit_button').click(function(){ 
        ShowCustomDialog(); 
       }); 
        }); 

     function ShowCustomDialog() 
     { 
      var name = document.getElementById('name'); 
      var address = document.getElementById('email'); 
      var reason = document.getElementById('reason'); 
      var message = document.getElementById('message'); 
      if(name.value == ''){ 
       ShowDialogBox('Warning','Enter your name','Ok','', 'GoToAssetList',null); 
       return false; 
      } 
      if(email.value == ''){ 
       ShowDialogBox('Warning','Enter your email.','Ok','', 'GoToAssetList',null); 
       return false; 
      } 
      if(reason.value == ''){ 
       ShowDialogBox('Warning','Select a reason','Ok','', 'GoToAssetList',null); 
       return false; 
      } 
      if(message.value == ''){ 
       ShowDialogBox('Warning','Enter a message.','Ok','', 'GoToAssetList',null); 
       return false; 
      } 

     } 

     function ShowDialogBox(title, content, btn1text, btn2text, functionText, parameterList) { 
      var btn1css; 
      var btn2css; 

      if (btn1text == '') { 
       btn1css = "hidecss"; 
      } else { 
       btn1css = "showcss"; 
      } 

      if (btn2text == '') { 
       btn2css = "hidecss"; 
      } else { 
       btn2css = "showcss"; 
      } 
      $("#lblMessage").html(content); 

      $("#dialog").dialog({ 
       resizable: false, 
       title: title, 
       modal: true, 
       width: '400px', 
       height: 'auto', 
       bgiframe: false, 
       hide: { effect: 'scale', duration: 400 }, 

       buttons: [ 
           { 
            text: btn1text, 
            "class": btn1css, 
            click: function() { 

             $("#dialog").dialog('close'); 

            } 
           }, 
           { 
            text: btn2text, 
            "class": btn2css, 
            click: function() { 
             $("#dialog").dialog('close'); 
            } 
           } 
          ] 
      }); 
     }</script><form method="post" action="MAILTO:me" enctype="text/plain" onsubmit=" return ShowCustomDialog()"> 

    <div class="row"> 
    <label>Your Name:</label> 
    <input type="text" id="name" name="name" size="20" /> 
    </div> 
    <div class="row"> 
    <label>Your E-mail:</label> 
    <input type="text" id="email" name="email" size="20" /> 
    </div> 
    <div class="row"> 
    <label>Reason for Report:</label> 
    <select id="reason" name="reason" /> 
    <option value="">Please Select...</option> 
    <option value="bug">Bug Report</option> 
    <option value="feature">Feature</option> 
    <option value="tech_support">Technical Support</option> 
    <option value="other">Other...</option> 
    </select> 

    </div> 

    <div class="row"> 
    <label>Your Message:</label> 
    <textarea type="text" id="message" name="message" rows="7" cols="30"></textarea> 
    </div> 
    <input id="submit_button" type="submit" value="Send E-mail" /> 
    <div id="dialog" title="Alert message" style="display: none"> 
    <div class="ui-dialog-content ui-widget-content"> 
     <p> 
     <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0"></span> 
     <label id="lblMessage"> 
     </label> 
     </p> 
    </div> 
    </div> 
</form> 

我会任何帮助

回答

0

感激当您在一个按钮有一个点击事件,你必须返回false或使用e.preventDefault()。否则,按钮会提交页面,并且您从不会看到对话框。

例如

$(document).ready(function(){ 
      $('#submit_button').click(function(e){ 
      if(!ShowCustomDialog()) { 
       e.preventDefault() 
      } 
      }); 
}); 

用我的例子,你应该添加到您的ShowCustomDialog函数返回真。

function ShowCustomDialog() 
{ 
    ... 
    if(message.value == ''){ 
    ShowDialogBox('Warning','Enter a message.','Ok','', 'GoToAssetList',null); 
    return false; 
    } 

    return true; 
} 
+0

对不起警报( '你按下......') ; ... 我最初在测试后忘了把它拿出来 – user3688675

+0

感谢您的帮助,@罗伯特。但是,由于某些原因,当我运行它时,对话窗口不会显示在网页上,只能通过在线编译器。 – user3688675

+0

它为我工作:https://jsfiddle.net/wko763cd/。你是否在'$('#submit_button')行中添加了e。click(function(e){'? – Robbert