-3
<div id="dept-or-user-div" class="panel" style="padding-bottom:10px;">
    <h3 class="div-title">Select Alert Recipients</h3>
     <div class="recipient-error" style="display:none;"><p style="padding-top:10px; color:#f00;">Please select at least one recipient</p></div>

    <p>
        <input type="radio"  checked value="dept" name="user-or-dept">Department</input>
        <input type="radio"  value="user" name="user-or-dept">User</input>
        <input type="radio"  value="both" name="user-or-dept">Both</input>
    </p>
    <div class="department-select" class="dept">
        <p><label for="department-choice">Department:</label>
         <select name="department-choice" id="department-choice">
            <option></option>
            <?php
                echo get_departments();
            ?>
        </select></p>
    </div><!--end #department-select-->
    <div class="user-select user" >
        <p><label for="user-choice">User:</label> 

        <select name='user-choice' id="user-choice">
            <option></option>
            <?php
                echo get_users('option');
            ?>
        </select></p>

    </div><!--end #user-select--> 

</div><!--end #dept-or-user-div-->

这里我有三个单选按钮

  1. 部门(部门下拉菜单)
  2. 用户(用户下拉菜单)
  3. 两者(用户和部门 dropwon)

And what I want to do this when radio button with value "Both" ie third radio button is selected, I want to call a js function and dynamically populate users dropdown depending department value. 我怎样才能做到这一点?

4

1 回答 1

1

给它一个id,

 <input type="radio"  id="both" value="both" name="user-or-dept">Both</input>

然后,使用 Jquery ......做这样的事情......

 $('input#both').change(function(){

 if($(this).is(':checked')) {

 //do your ajax call here to retrieve values from database based on whatever criteria
 //and populate the result wherever you want
 $.ajax({
 url: "getdept.php",
 type: "POST",
 data: {type: "both"},
 dataType: "html",
 success: function(result){
  $('#department-choice').html(result);
 }
 }); 

}}

然后在那个php文件中,像......

$both = mysql_real_escape_string($_POST['type']);

$sql = "SELECT * FROM table WHERE dept = $both";
//dunno what your query will be, because I have no idea of your database
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
  echo "<option>".$row['dept']."</option>";
}

这将以...的形式输出

 <option>blah blah</option>
 <option>blah blah</option>
 <option>blah blah</option>

它将使用上面的 ajax 插入到select, 下拉列表中。

于 2013-07-16T15:36:08.787 回答