Javascript 如果未选中任何单选按钮组,则发出警报
这是我的评估表代码的一部分。我有10个单选按钮组。我似乎无法编写一段代码,如果用户未选中任何radiobutton组,将显示警报。当用户单击提交按钮时,将显示警报 身体{ 背景色:白色; } .气垫床{ 宽度:100%; 边界塌陷:塌陷; } .气垫床{ 填充:7px; 边框:4e95f4 1px实心; } /*定义所有表行的默认颜色*/ .气垫床{ 背景:ffffff; } /*定义表格行的悬停高亮显示颜色*/ .hoverTable tr:悬停{ 背景色:f9d71c; } 桌子 th, 运输署{ 边框:1px纯黑; 边界塌陷:塌陷; } th, 运输署{ 填充:10px; 文本对齐:居中; } 您现在正在评估AE-SE等级 能力 元素 1. 2. 3. 4. 5.Javascript 如果未选中任何单选按钮组,则发出警报,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这是我的评估表代码的一部分。我有10个单选按钮组。我似乎无法编写一段代码,如果用户未选中任何radiobutton组,将显示警报。当用户单击提交按钮时,将显示警报 身体{ 背景色:白色; } .气垫床{ 宽度:100%; 边界塌陷:塌陷; } .气垫床{ 填充:7px; 边框:4e95f4 1px实心; } /*定义所有表行的默认颜色*/ .气垫床{ 背景:ffffff; } /*定义表格行的悬停高亮显示颜色*/ .hoverTable tr:悬停{ 背景色:f9d71c; } 桌子 th,
关系和网络 建立关系和网络 避免与团队中的其他成员互动 与团队成员互动 努力参与跨层次的对话 影响 表达 不能清晰地表达想法和想法 能够在团队环境中工作并支持其他成员 专注于实现团队目标并接受他人的意见
我想这回答了你的问题 您可以单击submit按钮调用函数,也可以使用form submit event调用函数来检查单选按钮验证,并编写一个函数来验证单选按钮,如下所示
function check_validation(){
var a = $('input[type="radio"][name="a"]:checked').val();
var b = $('input[type="radio"][name="b"]:checked').val();
if(a===undefined){
alert('a group');
}
if(b===undefined){
alert('b group');
}
}
我添加了一个提交按钮,并在上面添加了一个点击事件。这是我的jquery代码
<h1 align="center">You are now evaluating AE-SE grade</h1>
<table class="hoverTable" bgcolor="white">
<colgroup>
<col style="background-color:#a9a9a9">
</colgroup>
<tr bgcolor="#a9a9a9">
<th>Competencies</th>
<th>Elements</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
<tr>
<p id="choices">
<td rowspan="1"><b>Relationship and networking</b></td>
<td><i>Building relations and network</i></td>
<td><input type="radio" name="a" value="1" />Refrains from interacting with other members in the team</td>
<td><input type="radio" name="a" value="2" /></td>
<td><input type="radio" name="a" value="3" />Interacts with the team members</td>
<td><input type="radio" name="a" value="4" /></td>
<td><input type="radio" name="a" value="5" />Makes efforts to engage in conversations across hierarchy</td>
</tr>
<tr>
<td rowspan="1"><b>Influencing</b></td>
<td><i>Communication</i></td>
<td><input type="radio" name="b" value="1" />Unable to articulate ideas and thoughts</td>
<td><input type="radio" name="b" value="2" /></td>
<td><input type="radio" name="b" value="3" />Is able to work in a team environment and support other members</td>
<td><input type="radio" name="b" value="4" /></td>
<td><input type="radio" name="b" value="5" />Focuses on achieving group goals and accepts others opinions</td>
</tr>
</table>
<button type="button" class="submit">Submit</button>
你的JS在哪里?请添加相关的JS代码,我们可以看一下。我们在这里为您提供您已经尝试过的解决方案。
<h1 align="center">You are now evaluating AE-SE grade</h1>
<table class="hoverTable" bgcolor="white">
<colgroup>
<col style="background-color:#a9a9a9">
</colgroup>
<tr bgcolor="#a9a9a9">
<th>Competencies</th>
<th>Elements</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
<tr>
<p id="choices">
<td rowspan="1"><b>Relationship and networking</b></td>
<td><i>Building relations and network</i></td>
<td><input type="radio" name="a" value="1" />Refrains from interacting with other members in the team</td>
<td><input type="radio" name="a" value="2" /></td>
<td><input type="radio" name="a" value="3" />Interacts with the team members</td>
<td><input type="radio" name="a" value="4" /></td>
<td><input type="radio" name="a" value="5" />Makes efforts to engage in conversations across hierarchy</td>
</tr>
<tr>
<td rowspan="1"><b>Influencing</b></td>
<td><i>Communication</i></td>
<td><input type="radio" name="b" value="1" />Unable to articulate ideas and thoughts</td>
<td><input type="radio" name="b" value="2" /></td>
<td><input type="radio" name="b" value="3" />Is able to work in a team environment and support other members</td>
<td><input type="radio" name="b" value="4" /></td>
<td><input type="radio" name="b" value="5" />Focuses on achieving group goals and accepts others opinions</td>
</tr>
</table>
<button type="button" class="submit">Submit</button>
body {
background-color: White;
}
.hoverTable {
width: 100%;
border-collapse: collapse;
}
.hoverTable td {
padding: 7px;
border: #4e95f4 1px solid;
}
/* Define the default color for all the table rows */
.hoverTable tr {
background: ##ffffff;
}
/* Define the hover highlight color for the table row */
.hoverTable tr:hover {
background-color: #f9d71c;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 10px;
text-align: center;
}
$(document).ready(function()
{
var radio=$("[type='radio']");
$(".submit").on("click",function(){
for(let i=0;i<radio.length;i++)
{
let status=$(radio[i]).is(":checked");
if(status === false)
{
alert("Radio button is not checked");
break;
}
}
})
})