我有四个单选按钮冷、暖、事件、全部。它显示为按钮。如果我单击冷按钮,值“冷”将传递给查询,反之亦然。基于单选按钮查询会更改。
我想显示记录的冷模式、暖模式或事件模式,因此我使用单选按钮。
HTML
<input type="radio" name="sta_choice" id="o1" value="Cold" onclick="handleClick(this.val);"><span>Cold</span>
<input type="radio" name="sta_choice" id="o2" value="Warm" onclick="handleClick(this.val);"><span>Warm</span>
<input type="radio" name="sta_choice" id="o3" value="Active" onclick="handleClick(this.val);"><span>Active</span>
<input type="radio" name="sta_choice" value="All" checked><span>All</span>
<div class="ref">
</div>
SQL
$sql="select * from client where active=0 and comp_id='$comp' order by c_name asc limit $i,5";
我的问题是我想将单选按钮的值动态传递给查询
并重新加载 ref div
如果我选择冷,我只显示冷值。
最佳答案
我会使用 jQuery 来做这样的事情。
获取单选选项的值(您提供的代码中唯一可用的“标识符”),并通过 AJAX 将该值发送到脚本,以作为参数包含在您的查询中。最后,在“ref”div中输出查询结果
<script>
$(':radio').on("click", function(e) {
e.preventDefault(e);
alert(this.value); // for testing
$.ajax({
type: "POST",
url: "processor.php",
cache: false,
data: { action: this.value },
error: function(msg) {
alert(msg);
},
success: function(text) {
alert(text); // for testing
$(".ref").html("<p>" + text + "</p>");
}
});
});
</script>
PHP:
<?php
echo 'You sent me '.$_POST['action'];
?>
关于javascript - 将单选按钮值传递给 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935078/