gpt4 book ai didi

How to check if a radio button has a value (has been checked at least one) on click of a button(如何在单击按钮时检查单选按钮是否有值(至少选中了一个))

转载 作者:bug小助手 更新时间:2023-10-25 12:00:14 25 4
gpt4 key购买 nike



I need to check if at least one radio button has been checked when I click the button to continue to the next step of the form. I need it to be dynamic, so I can use it for all the fields I need, and I can't figure it out why it's not working.
Please check the code, I need to check the value so I can use it for all type of fields. I know it works if I enter the radio button name, but I need it dynamic.
Thank you

我需要检查是否至少有一个单选按钮已被选中,当我点击按钮继续下一步的形式。我需要它是动态的,这样我就可以把它用在我需要的所有领域,我不知道为什么它不工作。请检查代码,我需要检查的值,所以我可以使用它的所有类型的字段。我知道它的作品,如果我输入单选按钮的名称,但我需要它的动态。谢谢你


<script>
function nextStep2(id) {
var fieldname = $("#"+id).attr("name");
var nomecampo = "m_"+fieldname;

if (frmMain.nomecampo.value=="") {
alert("Please select an answer to continue.");
return;
}

$("#step" + current_step).fadeOut(200);
$("#step" + (current_step + 1)).delay(200).fadeIn(200);
current_step ++ ;
}
</script>

/* MY HTML */
<form id="frmMain" name="frmMain">
<input type="radio" class="noradio" name="m_question_step1" id="m_question_step1-1" value="yes">
<label class="radio-inline" for="m_question_step1-1"><span>Yes</span></label>

<input type="radio" class="noradio" name="m_question_step1" id="m_question_step1-2" value="yes">
<label class="radio-inline" for="m_question_step1-2"><span>Yes</span></label>

<button id="bottone1" class="button btn-full btnlight" type="button" name="question_step1" onclick="nextStep2(this.id);">Continue</button>
</form>

更多回答
优秀答案推荐

If I understand you correctly, what you want to do is something like this:

如果我没有理解错的话,你想做的事情是这样的:


I changed the button to a submit button (for sermantic purposes).

我将按钮更改为提交按钮(出于宗教目的)。


<button type="submit">Continue</button>

$('form').on('submit', (e) => {
e.preventDefault();
var formIsValid = false;
$('input[type="radio"]').each(function(){
if($(this).is(':checked')){
formIsValid = true;
return
}
})
if(formIsValid){
$("#step" + current_step).fadeOut(200);
$("#step" + (current_step + 1)).delay(200).fadeIn(200);
current_step ++ ;
}else{
alert("Please select an answer to continue.");
}
})


In your code, frmMain.nomecampo.value won't work because nomecampo is just a string containing the name of the radio button group, not a reference to the actual radio buttons. Try this:

在您的代码中,frmMain.onomecampo.value不起作用,因为onomecampo只是一个包含单选按钮组名称的字符串,而不是对实际单选按钮的引用。试试这个:


<script>
function nextStep2(id) {
var fieldname = $("#" + id).attr("name");
var selectedRadio = $("input[name='" + fieldname + "']:checked");

if (selectedRadio.length === 0) {
alert("Please select an answer to continue.");
}
else {
$("#step" + current_step).fadeOut(200);
$("#step" + (current_step + 1)).delay(200).fadeIn(200);
current_step++;
}
}
</script>

Im using the :checked selector to filter only the checked radio buttons within that group.

我正在使用:Checked选择器只筛选该组中选中的单选按钮。


更多回答

thanks for your reply but I don't need to submit the form, just need to go to the next step, hence the fadeIn and fadeOut

谢谢你的回复,但我不需要提交表格,只需要进入下一步,因此出现了FADEIN和FADEOF

thanks for your reply, it works if I don't select any radio buttons (showing the alert) but it doesn't go to the next step if I check one of the radio buttons (still shows the alert)

谢谢你的回复,如果我没有选择任何单选按钮(显示警报),它会起作用,但如果我选中其中一个单选按钮(仍然显示警报),它不会进入下一步

Right, its because of the if statement. There should be an else statement. I have edited the code.

对,这是因为if语句。应该有一个Else声明。我已经编辑了代码。

still doesn't work, and I need to check if the value is empty, so I can use it for other fields too

仍然不起作用,我需要检查该值是否为空,这样我也可以将其用于其他字段

ok, it didn't work because we missed the "m_" in var currentField = $("input[name='m_" + fieldname + "']:checked"); but still I'd need it to be used for different types of input

好吧,它没有工作,因为我们在var CurrentField=$(“Input[name=‘m_”+fieldname+“’]:Checked”)中遗漏了“m_”;但是我仍然需要将它用于不同类型的输入

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