在ajax调用后使复选框变为cheked

时间:2016-12-02 09:30:52

标签: php ajax

我有一张表格。用户可以选择任何用户名。我进行ajax调用以显示用户的id,我想显示用户所在的部门。 这是我的表格:

<form id="reguserform" method="post" action="register.php#err">
<?php
    $res = verifyFormFields();
?>
<!-- Username field -->
<select class="empidselectbox" name="username" onchange="showUser(this.value)">
<option disabled selected>Username (required)</option>
<?php
    while ($row = mysqli_fetch_array($result))
    {
        echo "<option value='".$row[1]."'>$row[0]</option>";
    }
    mysql_free_result($result);
?>
</select>
<!-- Id field -->
<input type="hidden" name="ID" />
<div id="txtHint"><input type="text" class="input name" name="ID" placeholder="ID"  readonly/></div> 
<!-- Employee group field -->
<div class="group" style="float:left; width:60%; margin-left:20%; margin-bottom:10px;">
<input type='checkbox' style="padding-bottom:15px;" name="userGroup[]" value="1">Planning Department</input></br></br>
<input type='checkbox' style="padding-bottom:15px;" name="userGroup[]" value="2">HR Department</input></br></br>
<input type='checkbox' style="padding-bottom:15px;" name="userGroup[]" value="3">Recruitment Department</input></br></br>
</div>              
<center><input type="submit" class="button small" name="submit" value="Register User"/></center>
</form>

然后是ajax电话:

<script>
    function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                }
            };
            xmlhttp.open("GET","getuserAjax.php?q="+str,true);
            xmlhttp.send();
        }
    }
</script>

这是geruserAjax.php:

<?php

$result = getID();
$result2= getRole();

while($row = mysqli_fetch_array($result)) 
{
    echo "<input type='hidden' name='user' value='".$row['username']."'/>";
    echo "<input type='hidden' name='ID' />";
    echo "<input type='text' class='input name'  name='ID'  value='".$row['uid']."' readonly/>";

}

while($row2 = mysqli_fetch_array($result2)) 
{
    echo "<div class='group' style='float:left; width:60%; margin-left:20%; margin-bottom:10px;'>";
    if($row2['planDep'] == 1 && $row2['HRDep'] == 2 && $row2['RecDep'] == 3)
    {
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='1' checked>Planning Department</input></br></br>";      
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='2' checked>HR Department</input></br></br>";
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='3' checked>Recruitment Department</input></br></br>";
    }
    else if ($row2['planDep'] == 1)
    {
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='2' checked>HR Department</input></br></br>";
    }   
    else if ($row2['HRDep'] == 2)
    {
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='2' checked>HR Department</input></br></br>";
    }
    else if ($row2['RecDep'] == 3)
    {
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='3' checked>Recruitment Department</input></br></br>";
    }
    else
    {
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='1' >Planning Department</input></br></br>";     
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='2' >HR Department</input></br></br>";
        echo "<input type='checkbox' style='padding-bottom:15px;' name='userGroup[]' value='3' >Recruitment Department</input></br></br>";      
    }   
    echo "</div>";  
}
?>

问题是我正在检查复选框,但作为新字段。但我想用旧字段替换它们。我怎么能实现这个目标?

1 个答案:

答案 0 :(得分:0)

您可以将以下代码放入Ajax的成功回调中

document.getElementById(&#39; name_of_checkbox&#39;)。checked = true;