如何检查单选按钮数组是否已检查?

时间:2017-05-11 10:25:35

标签: html

以下是html内容,我想检查其中一个单选按钮是否已被选中?

$data = json_decode(file_get_contents("php://input"));  

if (count($data) > 0)  
{   
  $Code = mysqli_real_escape_string($connect, $data->Code);                 
  $query = "UPDATE sale
    SET 
    Num=Null WHERE Code='$Code'"; 
  if(mysqli_query($connect, $query))  
  {  
    echo "modification réussie ...";  
  }  
  else  
  {  
       echo 'Error';  
  }  
}

2 个答案:

答案 0 :(得分:1)

https://plnkr.co/edit/vlYNcBeEUkP0wLtcbqtV?p=preview

<!DOCTYPE html>
<html>

<body>

  <form>
    <div class="col-sm-4 rating">
      <input type="hidden" name="questionId[]" value="4" />
      <input type="radio" class="career_ratings" id="career_star31" name="career_rating_answer[3]" value="1" />
      <label for="career_star31" title="1">1</label>

      <input type="radio" class="career_ratings" id="career_star32" name="career_rating_answer[3]" value="2" />
      <label for="career_star32" title="2">2</label>

      <input type="radio" class="career_ratings" id="career_star33" name="career_rating_answer[3]" value="3" />
      <label for="career_star33" title="3">3</label>

      <input type="radio" class="career_ratings" id="career_star34" name="career_rating_answer[3]" value="4" />
      <label for="career_star34" title="4">4</label>
    </div>
    <button onclick="check()">Try it</button>

  </form>

  <script>
    function check() {
      var radio = document.getElementsByClassName('career_ratings');
      var x = false;
      for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
          x = true;
        }
      }
      alert(x)
    }
  </script>

</body>

</html>

答案 1 :(得分:0)

只需将所有无线电输入的名称更改为     名称= “career_rating_answer []”

然后你会得到例如在PHP $ _POST ['career_rating_answer']作为一个数组,可以用 $ _ POST ['career_rating_answer'] [0] 评估第一个, $ _ POST ['career_rating_answer'] [1] < / em>第二个等..