我想学习如何从数据库中搜索某些东西,这是有效的,这里是代码
<fieldset>
<form action="" method="POST">
By Round Id : <input id="input-round" type="text" name="name" size="15"/><br/><br/>
By Player Id : <input id="input-player-id" type="text" name="player" size="15"/><br/><br/>
<input id="btn-form-close" type="button" value="Cancel" />
</form>
<script type="text/javascript">
$("#btn-form-close").click(function(){
$('#view-form').html('');
});
$('#input-round').keyup(function(){
var name = $('#input-round').val();
$.ajax({
type:"POST",
url:"<?php echo site_url('account/look_search_history_round');?>",
data:'nama='+name,
success:function(html){
$('#view-list-round-id').html(html);
}
});
});
$('#input-player-id').keyup(function(){
var player = $('#input-player-id').val();
$.ajax({
type:"POST",
url:"<?php echo site_url('account/look_search_history_player');?>",
data:'nama='+player,
success:function(html){
$('#view-list-round-id').html(html);
}
});
});
但我想创建类似选项的选项,因此我可以选择搜索By Round Id
或By player Id
。但我不知道如何实现这一目标。我得到的是选择该选项
<select class="form-control" id="sel1">
<option> By Round Id </option>
<option> By Player Id</option>
</select>
但输入怎么样?有人可以给我一些例子,非常感谢
答案 0 :(得分:1)
首先,您可能希望在表单中包含:
<form action="" method="POST">
Search by
<select class="form-control" id="sel1">
<option id="roundId"> By Round Id </option>
<option id="playerId"> By Player Id</option>
</select>
Id : <input id="input-round" type="text" name="name" size="15"/><br/><br/>
<input id="btn-form-close" type="button" value="Cancel" />
</form>
然后将所选选项发送到后端进行处理
$('#input-round').keyup(function(){
var name = $('#input-round').val();
var choice = $('#sel1 option:selected').val()
$.ajax({
type:"POST",
url:"<?php echo site_url('account/look_search_history_round');?>",
data:'nama='+name+'&choice='+choice, //process this choice in backend..
success:function(html){
$('#view-list-round-id').html(html);
}
});
});
答案 1 :(得分:1)
您没有给出选项
的价值像这样改变
<select class="form-control" id="sel1">
<option value="1" data-id="2"> By Round Id </option>
<option value="2" data-id="1"> By Player Id</option>
<option value="3" data-id="2"> By Round Id </option>
</select>
和jquery函数
$('#sel1').change(function(){
var player = $(this).val();
var PR_id = $(this).find(':selected').attr('data-id');
if(PR_id == 1)
{
$.ajax({
type:"POST",
url:"<?php echo site_url('account/look_search_history_player');?>",
data:'nama='+player,
success:function(html){
$('#view-list-round-id').html(html);
}
});
}
else if(PR_id ==2)
{
//next ajax call
}
});
答案 2 :(得分:0)
感谢所有我现在得到它,这里是我想要的代码
<form action="" method="POST">
Search by
<select class="form-control" id="select">
<option id="roundId"> Rounds Id</option>
<option id="playerId"> Player Id</option>
</select>
: <input id="input" type="text" name="input" size="15"/><br/><br/>
<input id="submit" type="button" value="Search" />
</form>
像这样的jquery函数
<script type="text/javascript">
$("#btn-form-close").click(function(){
$('#view-form').html('');
});
$("#submit").click(function(){
var data = {
'select' : $("#select").val(),
'input' : $("#input").val(),
};
//alert ($("#input").val());
$.ajax({
type:"POST",
url:"<?php echo site_url('account/look_search_history');?>",
data:data,
success:function(html){
$('#view-list-round-id').html(html);
}
});
});
</script>
关于控制器在这里
public function look_search_history(){
if($_POST['input']){
$select = $_POST['select'];
$input = $_POST['input'];
if ($select == "Rounds Id"){
$data['tbl_bet_spot'] = $this->login_model->select_by_round($input)->result();
$this->load->view('roulette/view_search_history', $data);
}else if ($select == "Player Id"){
$data['tbl_bet_spot'] = $this->login_model->select_by_player($input)->result();
$this->load->view('roulette/view_search_history', $data);
}
}
}