php 添加验证以查看是否未选中单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19312459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:15:19  来源:igfitidea点击:

Adding validations to see if radio button is not selected

javascriptphphtml

提问by user2822187

I have the following code:

我有以下代码:

 <li>1. question 1</li>
<li>
     <input type="radio" name="question1" id="sd1" value="1">Strongly Disagree
     <input type="radio" name="question1" id="d1" value="2">Disagree
     <input type="radio" name="question1" id="n1" value="3">Neither Agree Nor Disagree
    <input type="radio" name="question1" id="a1" value="4">Agree
     <input type="radio" name="question1" id="sa1" value="5">Strongly Agree
</li>
<br/><br/>

<li>2.  question 2 </li>
<li>
     <input type="radio" name="question2" id="sd2" value="1">Strongly Disagree
     <input type="radio" name="question2" id="d2" value="2">Disagree
     <input type="radio" name="question2" id="n2" value="3">Neither Agree Nor Disagree
    <input type="radio" name="question2" id="a2" value="4">Agree
     <input type="radio" name="question2" id="sa2" value="5">Strongly Agree
</li>
<br/><br/>

<li>3.  question 3</li>
<li>
     <input type="radio" name="question3" id="sd3" value="1">Strongly Disagree
     <input type="radio" name="question3" id="d3" value="2">Disagree
     <input type="radio" name="question3" id="n3" value="3">Neither Agree Nor Disagree
     <input type="radio" name="question3" id="a3" value="4">Agree
     <input type="radio" name="question3" id="sa3" value="5">Strongly Agree
</li>
<br/><br/>

  <input type="submit" value="Submit" name="Submit" id="Submit" />

I have such 30 questions. My requirement is that the user must answer all 30 questions.

我有这样的30个问题。我的要求是用户必须回答所有 30 个问题。

How do I write javascript function such that a mesage is shown tot he user if he doesnt answer even one of the questions. EDIT:

我如何编写 javascript 函数,以便在用户没有回答任何一个问题时向他显示一条消息。编辑:

The problem with my javascript i sthis:

我的 javascript 的问题是:

   if ( (thisfrm.question3[0].checked == false) || (thisfrm.question3[1].checked == false) || (thisfrm.question3[2].checked == false) || (thisfrm.question3[3].checked == false) || (thisfrm.question3[4].checked == false))
{
    alert('Please answer question 1');
    return false;
}

the above code is repeated for every question without loop. i.e. it is written for each ques. but even when all ques are answered,it still displays Please answer question 1

上面的代码在没有循环的情况下对每个问题重复。即它是为每个问题编写的。但即使回答了所有问题,它仍然显示 Please answer question 1

回答by Seetpal singh

Below code is more simple and clean for even biggners

下面的代码对于更大的人来说更简单和干净

if (!$("input[name='html_elements']:checked").val()) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

check out this below fiddle demo

看看下面这个小提琴演示

Visit http://jsfiddle.net/creators_guru/DBd79/

访问http://jsfiddle.net/creators_guru/DBd79/

回答by Mina

This method makes use of jQuery and checks for unchecked radio buttons in a set of answers

此方法使用 jQuery 并检查一组答案中未选中的单选按钮

HTML - add a class to your questions <li>

HTML - 为您的问题添加一个类 <li>

<li>1. question 1</li>
<li class="option">
     <input type="radio" name="question1" id="sd1" value="1">Strongly Disagree
     <input type="radio" name="question1" id="d1" value="2">Disagree
     <input type="radio" name="question1" id="n1" value="3">Neither Agree Nor Disagree
     <input type="radio" name="question1" id="a1" value="4">Agree
     <input type="radio" name="question1" id="sa1" value="5">Strongly Agree
</li>

Javascript

Javascript

// Delegate submit action
$(document).on('submit', 'form', function () {

    var validate = true;
    var unanswered = new Array();

    // Loop through available sets
    $('.option').each(function () {
        // Question text
        var question = $(this).prev();
        // Validate
        if (!$(this).find('input').is(':checked')) {
            // Didn't validate ... dispaly alert or do something
            unanswered.push(question.text());
            question.css('color', 'red'); // Highlight unanswered question
            validate = false;
        }
    });

    if (unanswered.length > 0) {
        msg = "Please answer the following questions:\n" + unanswered.join('\n'); 
        alert(msg);
    }
    return validate;
});

Example here http://fiddle.jshell.net/6jNpQ/2/

这里的例子http://fiddle.jshell.net/6jNpQ/2/

回答by Dilantha

Check this. Instead of validating each element you can use something like this

检查这个。您可以使用类似这样的东西,而不是验证每个元素

for(var i = 1 ; i <= 30 ; i++)
  {
      var radios = document.getElementsByName('question'+i);
      var checked = false;
      for (var j = 0, length = radios.length; j < length; j++) 
      {

         if (radios[j].checked) 
         {
          checked = true;
          break;
         }


       }
       if(!checked)
       {
         alert('Please answer question '+i);
         break;
       }
}

回答by Telémako

It looks like you're using only javascript. I recommend you adding jQuery to make writing javascript easier.

看起来您只使用 javascript。我建议您添加 jQuery 以使编写 javascript 更容易。

Anyway, only using javascript you can try this:

无论如何,只使用 javascript 你可以试试这个:

var questions = 30;
var answer_checked = false;
var one_not_checked = false;
for (var i = 1; i <= questions; i++) {
    for (var j=0; j < document.getElementsByName('question'+i).length; j++) {
        answer_checked = answer_checked || (thisfrm['question'+i][j].checked == true);
    }
    one_not_checked = one_not_checked || !answer_checked;
    answer_checked = false;
}

if (one_not_checked) {
    // your message to the user as one answer is not checked
}

回答by Ruddy

Something like this?

像这样的东西?

if ($('input[type=radio]:checked').length <= 0) {
    alert("No radio checked")
}

Or do it by name?

还是按名字做?

if ($('input[name=question1]:checked').length <= 0) {
    alert("No radio checked")
}

回答by Tun Zarni Kyaw

You should use logical operator &&for your condition

您应该&&为您的条件使用逻辑运算符

if ( (thisfrm.question3[0].checked == false) && 
     (thisfrm.question3[1].checked == false) && 
     (thisfrm.question3[2].checked == false) && 
     (thisfrm.question3[3].checked == false) && 
     (thisfrm.question3[4].checked == false) )

回答by Rahul J. Rane

Here is the answer for your question. You can check thisalso.

这是您的问题的答案。你也可以检查这个

Just using this code I have write it for your code. Please see below

仅使用此代码,我就为您的代码编写了它。请看下面

HTML Code

HTML代码

<form method="POST" action="/echo/html/?html=;delay=3;">
    <ul>
        <li class="question">1) question 1</li>
        <li class="answers">
            <input type="radio" name="question1" id="11" value="1" />Strongly Disagree
            <input type="radio" name="question1" id="12" value="2" />Disagree
            <input type="radio" name="question1" id="13" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question1" id="14" value="4" />Agree
            <input type="radio" name="question1" id="15" value="5" />Strongly Agree
        </li>

        <li class="question">2) question 2</li>
        <li class="answers">
            <input type="radio" name="question2" id="21" value="1" />Strongly Disagree
            <input type="radio" name="question2" id="22" value="2" />Disagree
            <input type="radio" name="question2" id="23" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question2" id="24" value="4" />Agree
            <input type="radio" name="question2" id="25" value="5" />Strongly Agree
        </li>

        <li class="question">3) question 3</li>
        <li class="answers">
            <input type="radio" name="question3" id="31" value="1" />Strongly Disagree
            <input type="radio" name="question3" id="32" value="2" />Disagree
            <input type="radio" name="question3" id="33" value="3" />Neither Agree Nor Disagree
            <input type="radio" name="question3" id="34" value="4" />Agree
            <input type="radio" name="question3" id="35" value="5" />Strongly Agree
        </li>
    </ul>
    <input type="submit" value="Submit" name="Submit" id="Submit" /> 
</form>

Javascript Code

Javascript代码

$("form").submit(function () {
    var allChecked = true;
    $('li.answers').each(function () {
        if (!$(':radio', this).is(':checked')) {
            allChecked = false;
            return false;
        }
    });
    if (!allChecked) {
        alert('Please fill the form completely...');
        return false;
    } else {
        alert('You have filled the complete form.');
    }
});

回答by Diana

#by javascript

#by javascript

function validate(){
if (checkRadio("question1") && checkRadio("question2") && checkRadio("question3")){
 return true;
}else{
alert("Please answer all Questions!");
 return false;
}
}
function checkRadio(name){
 var radio = document.forms.myForm[name];
for (var option in radio){
if(radio[option].checked){
 return true;
}
}
return false;
}