- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
需要帮助来验证是否使用 Java 脚本至少选中了一个复选框。
工作流程:
1.用户选择复选框值
2.选择值后,用户点击保存按钮
如果未选择任何复选框值,则应发出警报以选择至少一个复选框值
这是代码
产品.jsp
<%@page import="java.util.List"%>
<%@page import="web.Products"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function Validate()
{
var x[]=document.forms["sform"]["prod"].value;
for(var i=0;i<x.length;i++){
if(x[i].type=='checkbox'&&x[i].checked==false)
{
alert("Please select the products available and click save");
return false;
}
}
}
</script>
</head>
<body>
<form name="sform" method="post" action="Save_Products" onsubmit="javascript:return Validate();">
<b>
Brand Name:<font color="green">
<% String brand_name=(String)session.getAttribute("brand_name");
out.print(brand_name);%>
<c:set var="brand_name" value="brand_name" scope="session" />
</font></b>
<table>
<tr>
<th> Products</th>
<th> Description </th>
</tr>
<tr>
<td> <b><%
List<Products> pdts = (List<Products>) request.getAttribute("list");
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"checkbox\" name=\"prod\" value=\"" + prod.getProductname() + "\">" + prod.getProductname()+"<br>");
} %> </b></td>
<td><%for(Products prod: pdts){
out.println("<input type=\"text\" name=\"desc\" style=\"width:50px; height:22px\"/><br/>");
}
}
%> </td>
</tr>
<br/>
<br/>
<tr><td align="center"> <input type="submit" value="Save" name="save"/> </td></tr>
</table>
</form>
</body>
</html>
最佳答案
您的 javascript Validate() 函数看起来有逻辑错误。
目前,只要您发现一个复选框为 false,您就会返回 false 并提醒用户。为了获得所需的结果,只有在检查所有复选框都为 false 后才应返回 false(如果只有一个复选框为 true,则返回 true)。
下面的函数可能与您正在寻找的函数很接近。
function Validate() {
var x[]=document.forms["sform"]["prod"].value;
for(var i=0;i<x.length;i++){
if(x[i].type=='checkbox'&&x[i].checked==true) {
return true;
}
}
alert("Please select the products available and click save");
return false;
}
编辑:更新了代码。声明 x 数组时出现语法错误(删除“[]”),我们需要将 x 初始化为整个数组,因此从末尾删除“.value”。
function Validate() {
var x=document.forms["sform"]["prod"];
for(var i=0;i<x.length;i++){
if(x[i].type=='checkbox'&&x[i].checked==true) {
return true;
}
}
alert("Please select the products available and click save");
return false;
}
关于javascript - 复选框验证-验证至少一项被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37486911/
我需要一个带有条件的正则表达式: 最少 6 个字符,最多 50 个字符 必须包含 1 个字母 必须包含 1 个数字 可能包含特殊字符,例如!@#$%^&*()_+ 目前我有模式:(?!^[0-9]*$
我想检查密码是否至少包含一个字母和一个数字。接受特殊字符但不是必需的... 那将是一个简单的密码检查器。 最佳答案 您可以使用先行断言来检查任何数字和任何字母的存在,如下所示: ^(?=.*[a-zA
我需要找到生产至少三种不同型号电脑的制造商。结果集:制造商、型号数量。 我的 table 是产品(制造商、型号、类型)PC(代码、型号、速度、内存、高清、CD、价格) 我现在已经这样做了 SELECT
在我正在处理的哈希字典中,根据 GDB,数组的大小已从 500 调整到 1000。它在尝试添加时崩溃的数组索引是 799,所以它没有超出范围......我不是确定它为什么会出现段错误,尤其是在这样一条
我需要一个正则表达式来检查字符串的长度是否至少为 10 个字符。这些字符是什么并不重要。 谢谢 最佳答案 您可以使用: .{10,} 自 .默认情况下不匹配换行符,您必须使用合适的修饰符(如果您的正则
我有以下代码: val num1: Int? = someObject.number val num2: Int? = anotherObject?.anotherNumber val numToFa
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我一直在进行查询以获取用户列表及其各自的组。我知道了。 但是有一些限制: 如果用户只是 group5 的成员,则不应出现在列表中 如果是 group5 和其他组的成员,它应该出现在列表中 我可以获得不
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我需要忽略任何一个空格,并且应该匹配至少大于一个空格的空格... "MARY HAD A LITTLE LAMB" 我希望 "MARY", "HAD A LITTLE", "
如何在仅数字验证中允许空格(至少 8 位数字)?当允许空格时,输入电话号码会更容易。例如0400 123 456、9699 1234。 到目前为止,这是我的代码,我只进行了最少 8 位数字验证: jQ
我想将以下内容重写为 HTML 模式: if (/\S/.test(myString)) { // string is not empty and not just whitespace }
我的想法是使用 php 或 js 创建大量的 div。 (我从昨天开始写js。) 所以我自己给定的任务是使用 php 或 js 或两者生成一个 div 网格。到目前为止的想法是有一个通用的 div 作
显示字符串中至少有 10 个数字字符的正则表达式。 可以多于 10 个,但不能少于 10 个。在随机位置可以有任意数量的其他字符,将数字分隔开。 示例数据: (123) 456-7890 123-45
我目前在 Firefox 中使用边框动画时遇到问题,从无到 50px。 Chrome 非常好,但 Firefox 不是,我想同样的问题也会出现在 Opera 中。 这里有一个 fiddle 来向您展示
作为 ruby 挑战训练的一部分,我被要求创建一个方法 substrings(string),它将接受一个字符串,并返回一个包含所有子字符串的数组。 即 substrings("cat") #=>
我目前有这段代码,它检查数组中的所有元素是否相同。如果是这种情况,则返回 true def all_equal(lst): """ >>> all_equal([1,1,1,1,1,1,1])
当我注意到 Libre Office 电子表格显示远低于 2^53 的数字的错误值时,我正在做一些计算以计划改进我的主要筛选器的实现,这是 FoxPro 和许多其他精确整数计算的限制内部使用 C do
我有一个复选框,我想检查用户是否填写了它后面的文本框。必须至少包含 5 个字符才能选中复选框。 pro)) echo 'checked' ?>> pro?>">Please supply your I
我正在为我的食谱设计一个数据库。我在我的设计中创建了多个表格:书籍、作者、食谱、成分,对于所有这些项目,我想将媒体(图像或视频)链接到所有这些表格中的项目。 我在想这样的设计: media_id, r
我是一名优秀的程序员,十分优秀!