- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要验证单选按钮输入,即当按下提交按钮并且未选择单选按钮时,它会提醒用户“请选择选项”,如果选择了单选按钮,则只需提交表单并且页面重定向到另一个页面,不需要警报。但在我的代码中,每当我按下提交按钮时。即使选择了所有单选按钮,它也会显示“请选择选项”。请帮忙。
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>PVQ Questionary</title>
<!-- =========================================================== -->
<script LANGUAGE="JavaScript">
function validateForm() {
var radios = document.getElementsByName("yes no");
var formValid = false;
var i = 0;
while (!formValid && i < radios.length) {
if (radios[i].checked) formValid = true;
i++;
}
if (!formValid) alert("Must check some option!");
return formValid;
}
</script>
<!-- =========================================================== -->
</head>
<body>
<form name="form1" action="process.php" onsubmit="return validateForm()" method="post">
<!-- <form method="post" action="process.php"> -->
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<label>Location</label>
<input type="text" name="location" />
<br />
<label>Sex</label>
<input type="radio" name="sex" value="male" >Male
<br/>
<div align="center">
<FONT size="" color="blue">
<h1>Portrait Values Questionnaire (PVQ) for Male </h1>
<h2> Here we briefly describe some people. Please read each description and think about how much each person is or is not like you. Put an "Yes" or "No" in the given options that shows how much the person in the description is like you.</h2>
</div>
</FONT>
<h3>1. Thinking up new ideas and being creative is important to him. He likes to do things in his own original way.</h3>
<input type="radio" name="answer1" value="yes" >Yes
<input type="radio" name="answer1" value="no">No
<br/>
<h3>2. It is important to him to be rich. He wants to have a lot of money and expensive things.</h3>
<input type="radio" name="answer2" value="yes" >Yes
<input type="radio" name="answer2" value="no">No
<br/>
<h3>3. He thinks it is important that every person in the world be treated equally. He believes everyone should have equal opportunities in life.</h3>
<input type="radio" name="answer3" value="yes" >Yes
<input type="radio" name="answer3" value="no">No
<br/>
<h3>4. It's very important to him to show his abilities. He wants people to admire what he does.</h3>
<input type="radio" name="answer4" value="yes" >Yes
<input type="radio" name="answer4" value="no">No
<br/>
<h3>5.It is important to him to live in secure surroundings. He avoids anything that might endanger his safety.</h3>
<input type="radio" name="answer5" value="yes" >Yes
<input type="radio" name="answer5" value="no">No
<br/>
<h3>6.He thinks it is important to do lots of different things in life. He always looks for new things to try.</h3>
<input type="radio" name="answer6" value="yes" >Yes
<input type="radio" name="answer6" value="no">No
<br/>
<h3>7.He believes that people should do what they're told. He thinks people should follow rules at all times, even when no-one is watching.</h3>
<input type="radio" name="answer7" value="yes" >Yes
<input type="radio" name="answer7" value="no">No
<br/>
<h3>8.It is important to him to listen to people who are different from him. Even when he disagrees with them, he still wants to understand them.</h3>
<input type="radio" name="answer8" value="yes" >Yes
<input type="radio" name="answer8" value="no">No
<br/>
<h3>9.He thinks it's important not to ask for more than what you have. He believes that people should be satisfied with what they have.</h3>
<input type="radio" name="answer9" value="yes" >Yes
<input type="radio" name="answer9" value="no">No
<br/>
<h3>10.He seeks every chance he can to have fun. It is important to him to do things that give him pleasure.</h3>
<input type="radio" name="answer10" value="yes" >Yes
<input type="radio" name="answer10" value="no">No
<br/>
<h3>11.It is important to him to make his own decisions about what he does. He likes to be free to plan and to choose his activities for himself.</h3>
<input type="radio" name="answer11" value="yes" >Yes
<input type="radio" name="answer11" value="no">No
<br/>
<h3>12.It's very important to him to help the people around him. He wants to care for their well-being.</h3>
<input type="radio" name="answer12" value="yes" >Yes
<input type="radio" name="answer12" value="no">No
<br/>
<h3>13.Being very successful is important to him. He likes to impress other people.</h3>
<input type="radio" name="answer13" value="yes" >Yes
<input type="radio" name="answer13" value="no">No
<br/>
<h3>14.It is very important to him that his country be safe. He thinks the state must be on watch against threats from within and without.</h3>
<input type="radio" name="answer14" value="yes" >Yes
<input type="radio" name="answer14" value="no">No
<br/>
<h3>15.He likes to take risks. He is always looking for adventures.</h3>
<input type="radio" name="answer15" value="yes" >Yes
<input type="radio" name="answer15" value="no">No
<br/>
<h3>16.It is important to him always to behave properly. He wants to avoid doing anything people would say is wrong.</h3>
<input type="radio" name="answer16" value="yes" >Yes
<input type="radio" name="answer16" value="no">No
<br/>
<h3>17.It is important to him to be in charge and tell others what to do. He wants people to do what he says.</h3>
<input type="radio" name="answer17" value="yes" >Yes
<input type="radio" name="answer17" value="no">No
<br/>
<h3>18.It is important to him to be loyal to his friends. He wants to devote himself to people close to him.</h3>
<input type="radio" name="answer18" value="yes" >Yes
<input type="radio" name="answer18" value="no">No
<br/>
<h3>19.He strongly believes that people should care for nature. Looking after the environment is important to him.</h3>
<input type="radio" name="answer19" value="yes" >Yes
<input type="radio" name="answer19" value="no">No
<br/>
<h3>20.Religious belief is important to him. He tries hard to do what his religion requires.</h3>
<input type="radio" name="answer20" value="yes" >Yes
<input type="radio" name="answer20" value="no">No
<br/>
<h3>21.It is important to him that things be organized and clean. He really does not like things to be a mess.</h3>
<input type="radio" name="answer21" value="yes" >Yes
<input type="radio" name="answer21" value="no">No
<br/>
<h3>22. He thinks it's important to be interested in things. He likes to be curious and to try to understand all sorts of things.</h3>
<input type="radio" name="answer22" value="yes" >Yes
<input type="radio" name="answer22" value="no">No
<br/>
<h3>23. He believes all the worlds’ people should live in harmony. Promoting peace among all groups in the world is important to him.</h3>
<input type="radio" name="answer23" value="yes" >Yes
<input type="radio" name="answer23" value="no">No
<br/>
<h3>24.He thinks it is important to be ambitious. He wants to show how capable he is.</h3>
<input type="radio" name="answer24" value="yes" >Yes
<input type="radio" name="answer24" value="no">No
<br/>
<h3>25.He thinks it is best to do things in traditional ways. It is important to him to keep up the customs he has learned.</h3>
<input type="radio" name="answer25" value="yes" >Yes
<input type="radio" name="answer25" value="no">No
<br/>
<h3>26.Enjoying life's pleasures is important to him. He likes to 'spoil' himself.</h3>
<input type="radio" name="answer26" value="yes" >Yes
<input type="radio" name="answer26" value="no">No
<br/>
<h3>27.It is important to him to respond to the needs of others. He tries to support those he knows.</h3>
<input type="radio" name="answer27" value="yes" >Yes
<input type="radio" name="answer27" value="no">No
<br/>
<h3>28.He believes he should always show respect to his parents and to older people. It is important to him to be obedient.</h3>
<input type="radio" name="answer28" value="yes" >Yes
<input type="radio" name="answer28" value="no">No
<br/>
<h3>29.He wants everyone to be treated justly, even people he doesn't know. It is important to him to protect the weak in society.</h3>
<input type="radio" name="answer29" value="yes" >Yes
<input type="radio" name="answer29" value="no">No
<br/>
<h3>30.He likes surprises. It is important to him to have an exciting life.</h3>
<input type="radio" name="answer30" value="yes" >Yes
<input type="radio" name="answer30" value="no">No
<br/>
<h3>31.He tries hard to avoid getting sick. Staying healthy is very important to him.</h3>
<input type="radio" name="answer31" value="yes" >Yes
<input type="radio" name="answer31" value="no">No
<br/>
<h3>32.Getting ahead in life is important to him. He strives to do better than others.</h3>
<input type="radio" name="answer32" value="yes" >Yes
<input type="radio" name="answer32" value="no">No
<br/>
<h3>33.Forgiving people who have hurt him is important to him. He tries to see what is good in them and not to hold a grudge.</h3>
<input type="radio" name="answer33" value="yes" >Yes
<input type="radio" name="answer33" value="no">No
<br/>
<h3>34.It is important to him to be independent. He likes to rely on himself.</h3>
<input type="radio" name="answer34" value="yes" >Yes
<input type="radio" name="answer34" value="no">No
<br/>
<h3>35.Having a stable government is important to him. He is concerned that the social order be protected.</h3>
<input type="radio" name="answer35" value="yes" >Yes
<input type="radio" name="answer35" value="no">No
<br/>
<h3>36.It is important to him to be polite to other people all the time. He tries never to disturb or irritate others.</h3>
<input type="radio" name="answer36" value="yes" >Yes
<input type="radio" name="answer36" value="no">No
<br/>
<h3>37.He really wants to enjoy life. Having a good time is very important to him.</h3>
<input type="radio" name="answer37" value="yes" >Yes
<input type="radio" name="answer37" value="no">No
<br/>
<h3>38. It is important to him to be humble and modest. He tries not to draw attention to himself.</h3>
<input type="radio" name="answer38" value="yes" >Yes
<input type="radio" name="answer38" value="no">No
<br/>
<h3>39. He always wants to be the one who makes the decisions. He likes to be the leader.</h3>
<input type="radio" name="answer39" value="yes" >Yes
<input type="radio" name="answer39" value="no">No
<br/>
<h3>40. It is important to him to adapt to nature and to fit into it. He believes that people should not change nature.</h3>
<input type="radio" name="answer40" value="yes" >Yes
<input type="radio" name="answer40" value="no">No
<br/>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
最佳答案
The thing is you have done the checking weather the radio is selected or not only once and which is to be done for all the available radio buttons.
for (i = 0; i < radios.length; i++) {
var radios = document.getElementsByName("yes no");
var formValid = false;
}
关于javascript - 如何使用 HTML 表单中的 javascript 检查单选按钮验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35422979/
我需要根据需要动态设置文本区域,但它不想正常工作。 JQuery 会自行检查,但无法检查是否已检查。但是当您在第二个单选框内单击时,始终需要文本区域。我尝试了很多次让它工作,但它仍然有问题。我添加了“
我需要在 Django 中进行 API 调用(某种形式),作为我们所需的自定义身份验证系统的一部分。用户名和密码通过 SSL 发送到特定 URL(对这些参数使用 GET),响应应该是 HTTP 200
我将在我的可移植 C 代码中使用 #warning 来生成编译时警告。但并非所有平台都支持 #warning。有什么方法可以找到该平台是否支持 #warning。 #ifdef warning
我编写了一个函数来检查某个数字是否存在于某个区间内。停止搜索的最佳方法是什么?这个: for (i = a; i <= b; i++) { fi = f(i); if (fi == c) {
我想知道在 c 中是否有一种方法可以检查,例如在 for 函数中,如果变量等于或不等于某些字符,而不必每次都重复进行相等性检查。如果我没记错的话,以这种方式检查相等性是不正确的: if (a == (
我有如下日志功能 void log_error(char * file_name, int line_num, int err_code) { printf("%s:%d:%s\n", fil
使用 ssh-keygen 生成的 key 对在 macOS 上可以有不同的格式。 macOS 可读的标准 PEM ASN.1 对象 SecKey API 带有文本标题的 PEM OpenSSH ke
我正在尝试编写一个 excel if 语句。我不熟悉使用 Excel 具有的所有额外功能。我正在使用一个名为 importXML() 的函数.我正在尝试检查我正在使用的函数是否生成“#VALUE!”错
有没有办法检查是否没有 AIO 写入给定文件?我在我的 Unix 类(class)上制作了一个项目,该项目将是一个上下文无关(基于 UDP)的国际象棋服务器,并且所有数据都必须存储在文件中。应用程序将
我有一个如下所示的函数: public Status execute() { Status status = doSomething(); if (status != Stat
我正在使用 Composer,我不希望 PhpStorm 在 vendor 文件夹上运行任何错误检查或检查,因为它对 vendor/中的某些代码显示误报composer/autoload_static
Chapel 的一个很好的特性是它区分了数组的域和它的分布。检查两个数组是否具有相同的域和分布(通常想要的)的最佳方法是什么? 我能看到的最好的方法是检查 D1==D2和 D1.dist==D2.di
在我的 JavaScript 函数中,我为所有输入、文本区域和选择字段提供实际值作为 initial_value: $('input, textarea, select').each(function
我正在编写一个分解为几个简单函数的 PHP 类。在构造函数中,它调用另一个名为 processFile 的函数。该函数调用 5 个私有(private)函数并进行检查。如果检查失败,它会将消息分配给
这个问题已经有答案了: How to detect if user it trying to open a link in a new tab? (2 个回答) 已关闭 7 年前。 我认为 JavaS
我正在浏览我们的代码库并看到很多这样的测试: declare @row_id int = ... declare @row_attribute string select @row_attribu
我正在声明一个用作比较的函数。我的问题是: 为什么条件充当语句? 为什么第 4 行可以工作,而第 5 行却不行? 我知道这段代码不切实际且未使用,但为什么编译器允许这种语法? 谷歌没有找到答案。但话又
到目前为止,我有一个带有空文本字段的 PHP Kontaktform,并使用以下命令检查了所需的字段: $name = check_input($_POST['name'], "请输入姓名。"); 现
目前,我能想到的合理检查的唯一方法没有臃肿的逻辑: if ( $value > 0 ) { // Okay } else { // Not Okay } 有没有更好的办法? 最佳答案
我正在尝试运行一个脚本,如果 i 存在(意味着存在 i 值,任何值)或其他部分,我希望运行其中的一部分如果i没有值就运行,有人可以启发我吗? 我说的是 for 循环,比如 for (var i=0;
我是一名优秀的程序员,十分优秀!