确认后禁用单选按钮

时间:2017-09-29 06:05:53

标签: javascript php

我尝试研究并尝试了很多选项,但仍然无法正常工作。用户单击单选按钮并确认后,我需要禁用单选按钮

<script type="text/javascript">
  function getVote(int) {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();

    } else { // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (confirm("Your vote is for " + int)) {

      xmlhttp.open("GET", "save.php?vote=" + int, true);
      xmlhttp.send();
      window.location.reload();

    } else {
      alert("Choose another candidate ");
    }

  }

  function getPosition(String) {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", "vote.php?position=" + String, true);

    xmlhttp.send();

  }
</script>

这是用户点击的单选按钮

     echo "<td><input type='radio' name='vote' value='$row[candidate_name]' onclick='getVote(this.value)' /></td>";

当用户点击单选按钮

时,这是save.php
   <?php
       require('connection.php');
       $vote = $_REQUEST['vote'];

   $checkposition=mysql_query("SELECT candidate_position FROM voting_tbCandidates WHERE candidate_name='$vote'");
     while ($row=mysql_fetch_array($checkposition)){
  session_start();

  if($row['candidate_position']=="PRESIDENT"){
  $_SESSION['vote_president']=$row['candidate_position'];
  }elseif($row['candidate_position']=="VICE"){
   $_SESSION['vote_vice']=$row['candidate_position'];
   }elseif($row['candidate_position']=="MUSE"){
    $_SESSION['vote_muse']=$row['candidate_position'];
    }elseif($row['candidate_position']=="SECRETARY"){
    $_SESSION['vote_secretary']=$row['candidate_position'];
    }elseif($row['candidate_position']=="ESCORT"){
     $_SESSION['vote_escort']=$row['candidate_position'];
   }elseif($row['candidate_position']=="AUDITOR"){
    $_SESSION['vote_auditor']=$row['candidate_position'];
      }
      }
     mysql_query("UPDATE voting_tbCandidates SET candidate_cvotes=candidate_cvotes+1 WHERE candidate_name='$vote'");

 mysql_close($con);
  ?> 

2 个答案:

答案 0 :(得分:0)

您只是将值发送回函数。将整个元素发送到函数并挑选出您需要的部件会更容易。例如: 改变

onclick='getVote(this.value)'

onclick='getVote(this)'

更改

function getVote(int) 

function getVote(obj) {
  var int = obj.value; 

xmlhttp.open("GET", "save.php?vote=" + int, true);添加行

之前
obj.setAttribute('disabled','disabled'); // I think this would work

答案 1 :(得分:0)

在确认时将disable属性设置为true,为else设置false。

修改后的功能应该是这样的

function getVote(obj) {
var int = obj.value;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();

} else { // code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

if (confirm("Your vote is for " + int)) {
  obj.disabled = true;
  //xmlhttp.open("GET","save.php?vote="+int,true);
  //xmlhttp.send();
  //  window.location.reload();

} else {
  obj.disabled = false;
  alert("Choose another candidate ");
}

}

并在函数调用期间传递input元素的对象

<td>
  <input type='radio' name='vote' value='45' onclick='getVote(this)'/>
</td>

这是工作小提琴:https://jsfiddle.net6mcd5bq7/