jquery Tools validator with overlay

爷,独闯天下 提交于 2019-12-11 09:24:37

问题


I'm getting some variant behaviour using jQuery Tools form dialog - i.e. using validator on a form contained in an overlay. Here is my code (should load straight up)

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Simple</title>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script src="http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js"></script>

  <style type="text/css">
.dialog {
  display: none;
  width: 400px;
  padding:5px;
  background-color:rgba(178, 168, 79, 0.7);
  border:2px solid #666;
  border:2px solid rgba(82, 82, 82, 0.7);
  -moz-border-radius:6px;
  -webkit-border-radius:6px;
}

form {
  background:#333;  
  padding:15px 20px; 
  color:#eee;
  width:360px;
  margin:0 auto;
  position:relative;
  -moz-border-radius:5px;
  -webkit-border-radius:5px;  
} 

form input {
  border:1px solid #444;
  background-color:#666;
  padding:5px;
  color:#ddd;
  font-size:12px;

  /* CSS3 spicing */
  text-shadow:1px 1px 1px #000;
  -moz-border-radius:4px;
  -webkit-border-radius:4px;  
}

form input:focus     { color:#fff; background-color:#777; }
form input:active   { background-color:#888; }

fieldset {
 border: 0;
}

.error {
  height:15px;
  background-color:#FFFE36;
  font-size:12px;
  border:1px solid #E1E16D;
  padding:4px 10px;
  color:#000;
  display:none;  

  -moz-border-radius:4px;
  -webkit-border-radius:4px; 
  -moz-border-radius-bottomleft:0;
  -moz-border-radius-topleft:0;  
  -webkit-border-bottom-left-radius:0; 
  -webkit-border-top-left-radius:0;

  -moz-box-shadow:0 0 6px #ddd;
  -webkit-box-shadow:0 0 6px #ddd;  
}

form label {
  display:block;
  font-size:13px;
  color:#ccc;
  padding-bottom: 2px;
}
  </style>
</head>

<body>
<button type="button" name="dlgBtn">Show Dialog</button>
<div class='dialog'>
  <form class='form'>
    <fieldset>
      <label>Name</label>
      <input name="name" type="text" required="required" />
    </fieldset>

    <button type="submit">Submit</button>
  </form>
</div>
</body>

<script>
$("button[name='dlgBtn']").click(function() {
  $("form")[0].reset();
  $("div").data("overlay").load();
}); 

$("form").validator().submit(function() {
  $("div").data("overlay").close();
  alert("Submitted OK!");
  return false;
});

$("div").overlay({top:180, closeOnClick: true, mask: {color: '#fff', loadSpeed: 100, opacity: 0.5}});
</script>
</html>

This code has the following problems (in order of importance to this question) -

  1. In Chrome 8.0.552.231, the 'required' validator failure does not stop the form being submitted (it does in FF).
  2. In Chrome, the error message is not visible in relation to the dialog, but appears in the document body, and is only visible once the dialog closes.
  3. In Firefox 4b7, the validator error appears below the input regardless of any position setting when initialising the validator. It should appear on the right by default.
  4. In FF, the form.reset() does not reset the form (it does in chrome). This can be tested by opening the dialog a second time after submitting. At first glance doesn't look to be jquery Tools related, but thought I'd throw it in :).
  5. In Chrome , the field border is not initially red (it is in FF)
  6. In FF, the field border does not turn blue when selected (it does in Chrome)
  7. The error message text for FF and Chrome is different

Other people seeing the same behaviour? Is it possible to fix that code to work as expected, and vaguely the same in both browsers? Cheers,


回答1:


Here is my initial try: example link

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Simple</title>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script src="http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js"></script>

        <style type="text/css">
            #dialog {
                display: none;
                width: 400px;
                padding:5px;
                background-color:rgba(178, 168, 79, 0.7);
                border:2px solid #666;
                border:2px solid rgba(82, 82, 82, 0.7);
                -moz-border-radius:6px;
                -webkit-border-radius:6px;
            }

            form {
                background:#333;
                padding:15px 20px;
                color:#eee;
                width:360px;
                margin:0 auto;
                position:relative;
                -moz-border-radius:5px;
                -webkit-border-radius:5px;
            }

            form input {
                border:1px solid #444;
                background-color:#666;
                padding:5px;
                color:#ddd;
                font-size:12px;

                /* CSS3 spicing */
                text-shadow:1px 1px 1px #000;
                -moz-border-radius:4px;
                -webkit-border-radius:4px;
            }

            form input:focus     { color:#fff; background-color:#777; }
            form input:active   { background-color:#888; }

            fieldset {
                border: 0;
            }

            .error {
                height:15px;
                background-color:#FFFE36;
                font-size:12px;
                border:1px solid #E1E16D;
                padding:4px 10px;
                color:#000;
                display:none;
                z-index: 9999;

                -moz-border-radius:4px;
                -webkit-border-radius:4px;
                -moz-border-radius-bottomleft:0;
                -moz-border-radius-topleft:0;
                -webkit-border-bottom-left-radius:0;
                -webkit-border-top-left-radius:0;

                -moz-box-shadow:0 0 6px #ddd;
                -webkit-box-shadow:0 0 6px #ddd;
            }
            .error p {margin: 0}
            form label {
                display:block;
                font-size:13px;
                color:#ccc;
                padding-bottom: 2px;
            }

            .hidden {display: none}
        </style>
    </head>

    <body>
        <button type="button" name="dlgBtn">Show Dialog</button>
        <div id='dialog'>
            <form id='form'>
                <fieldset>
                    <label>Name</label>
                    <input name="name" type="text" required="required" />
                </fieldset>
                <input type="reset" class="hidden" />
                <button type="submit">Submit</button>
            </form>
        </div>
    </body>

    <script>
        $("button[name='dlgBtn']").click(function() {
            $(":reset", "#form").click();
            $dialog.load();
        });

        // this is the object API and NOT the object itself!
        var $form = $("#form").validator({ position: 'center right' }).submit(function(e) {
            // Now this is the form object
            var form = $(this);

            // client-side validation OK.
            if (!e.isDefaultPrevented()) {
                // The ajax call; use the form object above to get the action attr, serialize..etc
                // more: http://flowplayer.org/tools/demos/validator/server-side.html
                //$.ajax({...})

                // BEGIN: THESE WILL GO IN THE SUCCESS METHOD OF THE AJAX CALL
                alert("Submitted OK!");
                $form.reset();
                $dialog.close();
                // END: THESE WILL GO IN THE SUCCESS METHOD OF THE AJAX CALL

                // THIS IS OUTSIDE THE AJAX CALL
                e.preventDefault();
            }
        }).data('validator');

        // this is the object API and NOT the object itself!
        var $dialog = $("#dialog").overlay({top:180, closeOnClick: true, mask: {color: '#fff', loadSpeed: 100, opacity: 0.5}}).data("overlay");

        // Without this, Error messages will remain visible
        $dialog.onBeforeClose(function(){
            $form.reset();
        });

        // This is to keep the error message next to it's corresponding input
        $(window).scroll(function() {
            $form.reflow();
        });
    </script>
</html>

EDIT: Answers to your questions added
1. In Chrome 8.0.552.231, the 'required' validator failure does not stop the form being submitted (it does in FF).
It does now
2. In Chrome, the error message is not visible in relation to the dialog, but appears in the document body, and is only visible once the dialog closes.
This is because you the overlay has z-index 9998, setting error div to 9999 fixed that
3. In Firefox 4b7, the validator error appears below the input regardless of any position setting when initialising the validator. It should appear on the right by default.
I don't know if it's a FF bug (unfortunately I don't hv FF 4), but I've added { position: 'center right' to the validator to insure the positioning
4. In FF, the form.reset() does not reset the form (it does in chrome). This can be tested by opening the dialog a second time after submitting. At first glance doesn't look to be jquery Tools related, but thought I'd throw it in :).
Having a hidden reset button will do the job and help the $form variable afterwords
5. In Chrome , the field border is not initially red (it is in FF)
It's not red in my FF...
6. In FF, the field border does not turn blue when selected (it does in Chrome)
Mostly likely the above two point are Browser (+ OS theme) related
7. The error message text for FF and Chrome is different
It's the same here..



来源:https://stackoverflow.com/questions/4515269/jquery-tools-validator-with-overlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!