1

我在www.thetotempole.ca有一个表单,我想知道当用户单击重置按钮时如何弹出一个窗口,询问他们是否真的想要这样做,如果不是,我希望表单中的文本字段不重置,如果是,我希望重置整个表单和文本字段中的所有文本。谢谢,因为我似乎无法让它工作!

HTML:

<html>
   <head>
      <script>
         function validate()
         {
            var myform = document.getElementById("form1");
            myform.fname.style.backgroundColor = "white";
            myform.lname.style.backgroundColor = "white";
            myform.address.style.backgroundColor = "white";
            myform.city.style.backgroundColor = "white";
            myform.provstate.style.backgroundColor = "white";
            myform.country.style.backgroundColor = "white";
            myform.postalzip.style.backgroundColor = "white";
            console.log("form object: " + myform);
            if (myform.fname.value == "")
            {
               alert("First Name must have a value");
               myform.fname.focus();
               myform.fname.style.backgroundColor = "red";
               return false;
            }
            if (myform.lname.value == "")
            {
               alert("Last Name must have a value");
               myform.lname.focus();
               return false;
            }
            if (myform.address.value == "")
            {
               alert("Address must have a value");
               myform.address.focus();
               return false;
            }
            if (myform.postalzip.value == "")
            {
               alert("Postal/Zip Code must have a value");
               myform.postalzip.focus();
               return false;
            }
            else
            {
               //var regex = new RegExp();
               var regex = /^([a-z]\d[a-z]\s?\d[a-z]\d)|(\d{5}(\s?\d{4})?)$/i;
               myform.postalzip.value.toUpperCase();
               if (!regex.test(myform.postalzip.value))
               {
                  alert("Postal/Zip Code has invalid format");
                  myform.postalzip.focus();
                  return false;

               }
            }

            return true;
         }
      </script>
   </head>
   <body>
      <form id="form1">
         First Name:<input type="text" name="fname" /><br />
         Last Name:<input type="text" name="lname" /><br />
         Address:<input type="text" name="address" /><br />
         City:<input type="text" name="city" /><br />
         Province/State:<input type="text" name="provstate" /><br />
         Country:<input type="text" name="country" /><br />
         Postal/Zip Code:<input style="text-transform: uppercase;" type="text" name="postalzip" /><br />
         <input type="submit" onclick="return confirm('Do you really want to continue?')" value="Send" />
         <input type="reset" onclick="onclick="return confirm('Do you really want to reset?')" value="Reset" />

      </form>
   </body>
</html>
4

1 回答 1

0

您的代码看起来有点乱码,onclick 方法里面有一个 onclick 位:

onclick="onclick="return confirm('Do you really want to reset?')"

如果你解决它,它有帮助吗?

于 2013-11-07T21:05:49.693 回答