将 confrim 添加到输入类型提交

如何解决将 confrim 添加到输入类型提交

我正在尝试向我的 input type="submit","re​​set" 按钮添加一个非常基本的确认对话框。我的代码片段看起来像这样。这是课堂作业。

脚本(我放置了 console.log 消息以查看是否/哪里出现故障)

<script>    
        function validate() {
            console.log("I am in the validate function");
            var response = confirm("Are you sure?");
            if (response = true){
                console.log("Ok was selected");
                return true;
                }
            else{
                console.log("Cancel as selected");
                return false;
            }
        }
    </script>

我的重置/提交输入代码:

<div class="row">
                <input type="submit" value="Submit" onClick="return validate()"/> <input type="reset" value="Reset" onClick="validate()"/>
            </div>

它们被格式化成一个带有大 div 容器的表单。所以现在,无论我单击确定还是取消,它都会执行按钮功能。我只希望它提交表单,或者如果单击“确定”则重置表单。所以控制台总是会收到一条消息,表明选择了 Ok,即使我点击了取消。我只学习了一天的 JS。我尝试了其他答案,但找不到确切的情况。我知道我缺少一些简单的东西,感谢您的帮助。

完整代码如下:

/*CSS Styles Specific to the Customer Registration Page Will Go Here*/
@import url('https://fonts.googleapis.com/css2?family=Cinzel+Decorative&display=swap');

#logo   {
    display: inline-block;
    margin: 15px;
    /*float: left;*/
    height: 100%;
    width: auto;
}

.footer { /*sets a custom font for my footer and automatically transforms chars to uppercase*/
    font-family: 'Cinzel Decorative',cursive;
    text-transform:uppercase;
    float: right;
    text-align: bottom right;
}
        
*{
box-sizing: border-box
} /*Makes all boxes sized as border boxes (so they apply padding etc)*/

input[type=text],select,textarea { /*makes all my input/drop down boxes have the same formatting*/
    width: 100%;
    padding: 12px;
    margin: 8px 0;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 4px;
    resize: vertical;
}
    
input[type=text]:focus { /*highlight the text input area of any clicked form field to similar to my logo color*/
    background-color: #48e0bd;
}

label { /*adds padding around all tags of type "label" in my form*/
    padding: 12px 12px 12px 0;
    display: inline-block;
}

input[type=submit],[type=reset] { /*applies color and other formatting to both reset and submit buttons*/
    background-color: #2b8c76;
    color: white;
    padding: 12px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    float: left;
}
    
input[type=submit]:hover,[type=reset]:hover { /*darkens reset or submite button when I hover over them*/
    background-color: #1f6656;
}

.container{
    border-radius: 0px;
    background-color: #f2f2f2;
    padding: 20px;
}

.col-25 {float: left;
         width: 25%;
         margin-top: 6px;
 }
         
 .col-75 {float: left;
         width: 75%;
         margin-top: 6px;
}
         
.row:after{ /*clears floats after columns*/
            content: "";
            display: table;
            clear: both;
}
/*Responsive layout for our registration page*/

@media screen and (max-width: 600px) {
    .col-25,.col-75,input[type=submit],[type=reset]{
        width: 100%;
        align-top: 0;
        }
}
<!DOCTYPE html>
<html>
    <head>
        <title>Grainy Travel: Plan Your Escape Customer Portal</title>
        <link rel="stylesheet" href="reset.css" /> <!-- This is a generic reset file off the web !-->
        
        <link rel="stylesheet" href="register.css" /> <!-- This is my custom CSS style sheet !--> 
        <script>    
            function validate() {
                console.log("I am in the validate function");
                var response = confirm("Are you sure?");
                if (response = true){
                    console.log("Ok was selected");
                    return true;
                    }
                else{
                    console.log("Cancel as selected");
                    return false;
                }
            }
        </script>
    </head>
    <body>
    <a href="#"><img id="logo" src="logo.png"></a> <br /> <!-- have to figure out how to separate this logo from the table! Figured it out be removing float: left--> 
    <!--this is a container that wraps my whole form field together. 
    Each following <div /> wraps my form "entries" by rows split into two columns,a 25 and 75 width column
    based on my register.css file. !-->
    <div class="container"> 
        <form method="post" action="bouncer.php">
            <h1>Enter Your Information:</h1>
                <div class="row"> 
                    <div class="col-25">
                        <label for="fname">First Name:</label>
                    </div>
                        <div class="col-75">
                            <input type="text" id="fname" name="fname" placeholder="Your name..." />
                        </div>
                </div>
                <div class="row"> 
                    <div class="col-25">
                        <label for="lname">Last Name:</label>
                    </div>
                        <div class="col-75">
                            <input type="text" id="lname" name="lname" placeholder="Your last name..." />
                        </div>
                </div>
                <div class="row"> 
                    <div class="col-25">
                        <label for="cadd">Street Address:</label>
                    </div>
                        <div class="col-75">
                            <input type="text" id="cadd" name="cadd" placeholder="Your street address..." />
                        </div>
                </div>
                <div class="row"> 
                    <div class="col-25">
                        <label for="cname">Your City:</label>
                    </div>
                        <div class="col-75">
                            <input type="text" id="cname" name="city" placeholder="The city of your town" />
                        </div>
                </div>
            <div class="row">
                <div class="col-25">
                    <label for="prov">Province:</label>
                </div>
                <div class="col-75">
                    <select id="prov" name="prov">
                        <option value="ab">Alberta</option>
                        <option value="bc">British Columbia</option>
                        <option value="mb">Manitoba</option>
                        <option value="nb">New Brunswick</option>
                        <option value="nl">Newfoundland and Labrador</option>
                        <option value="ns">Nova Scotia</option>
                        <option value="on">Ontario</option>
                        <option value="pe">Prince Edward Island</option>
                        <option value="qc">Quebec</option>
                        <option value="sk">Saskatchewan</option>
                        <option value="nt">Northwest Territories</option>
                        <option value="nu">Nunavut</option>
                        <option value="yt">Yukon</option>
                    </select>
                </div>
            </div>
                <div class="row"> 
                    <div class="col-25">
                        <label for="postcode">Your Postal Code:</label>
                    </div>
                        <div class="col-75">
                            <input type="text" id="postcode" name="postcode" placeholder="1N1N1N Format" />
                        </div>
                </div>
                <div class="row">
                    <input type="submit" value="Submit" onClick="return validate()"/> <input type="reset" value="Reset" onClick="validate()"/>
                </div>
        </form>
    </div> <br />
            
        <div class="footer"align="center";>
            Copyright 2021 &copy;
        </div>
    </body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-