Php 选择特定单选按钮时如何调用javascript?
这里有三个单选按钮 部门下拉列表 用户下拉列表 用户和部门都可以 我想做的是,当选择了值为二的单选按钮,即第三个单选按钮时,我想调用一个js函数,并根据部门值动态填充用户下拉列表。我该怎么做呢?给它一个idPhp 选择特定单选按钮时如何调用javascript?,php,javascript,html,Php,Javascript,Html,这里有三个单选按钮 部门下拉列表 用户下拉列表 用户和部门都可以 我想做的是,当选择了值为二的单选按钮,即第三个单选按钮时,我想调用一个js函数,并根据部门值动态填充用户下拉列表。我该怎么做呢?给它一个id <div id="dept-or-user-div" class="panel" style="padding-bottom:10px;"> <h3 class="div-title">Select Alert Recipients</h3>
<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-->
然后在php文件中,类似于
$('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);
}
});
}}
这将以…的形式输出
$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>";
}
它将插入到选择下拉列表中,使用上面的ajax。只需使用一个事件调用一个js函数,该函数将检查是否选中该函数,然后调用您想要运行的任何函数以及如何填充用户下拉列表?他有两个单选按钮,名称为class='user-or-dept'。使用带有值属性的选择器。非常感谢!因此,当选择部门时,我将在post中将该值传递给,并根据该值获取用户并将其填充为“用户选择”;是的,基本上……但你必须确保你的查询工作正常,我上面发布的wwhat就是一个例子,我不知道你的数据库结构,etcI建议绑定到更改事件,而不是单击事件,以防用户用键盘更改值。var department=$input[name='department-choice'].attr'value';这是在下拉列表中获取所选部门的正确方法吗?
$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>