gpt4 book ai didi

javascript - 如何在特定的 jquery 验证结构中验证 radio

转载 作者:行者123 更新时间:2023-11-28 09:25:51 27 4
gpt4 key购买 nike

你好,我正在编写一个多步骤表单,并且将表单的不同部分与一个带有 <fieldset> </fieldset> 的 jquery 脚本分开。标签

我的表单验证在该脚本上运行,看起来像这样

$(".next").click(function(){

var erros = 0;
//error checking in the first fieldset
if (fieldsetn == 1) {
//verify name
if ($('input[name="name"]').val().length ===0){
$('input[name="name"]').attr("placeholder","Please enter your name");
$('input[name="name"]').addClass('warning');


erros = 1;
}
else {
$('input[name="name"]').removeClass('warning');
}
//verify surname
if ($('input[name="surname"]').val().length ===0){
$('input[name="surname"]').attr("placeholder","Please enter your surname");
$('input[name="surname"]').addClass('warning');

erros = 1;
}
else {
$('input[name="surname"]').removeClass('warning');
}

但是我在验证 radio 场时遇到问题设置我的 radio 场 loks 是这样的:

 Chose the Logo pack <a href="services.html"  style="float:right" target="_blank">More Information about Logo Packs Pleas click here</a> 
<div id="emotion" name="emotion">
<input type="radio" name="emotion" id="basi" />
<label for="basi"><img src="images/basi.jpg" width="630px" alt="I'm sad" /></label><br>

<input type="radio" name="emotion" id="deli" />
<label for="deli"><img src="images/deli.jpg" width="630px" alt="I'm sad" /></label><br>

<input type="radio" name="emotion" id="premi" />
<label for="premi"><img src="images/premi.jpg" width="630px" alt="I'm happy" /></label>
</div>

如何验证我在 jquery 验证代码结构中设置的 radio 字段,以便必须选择一个选项?提前致谢

最佳答案

试试这个-

if($('input[name="emotion"]:checked').length==0)
{
alert("Please select radio button");
}

if(!$('input[name="emotion"]:checked').length)
{
alert("Please select radio button");
}

Demo

关于javascript - 如何在特定的 jquery 验证结构中验证 radio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25887375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com