[英]How to make dimanic input select option in chained dropdown option?
It's my first time i ask a question here. 这是我第一次在这里问一个问题。 i'm stuck here, so i hope someone can solved this problem.
我被困在这里,所以我希望有人可以解决这个问题。 thanks :)
谢谢 :)
I have some php code that i want to made a input select option adding automaticallly when clicked a button in my form. 我有一些php代码,当我单击表单中的按钮时,我想使输入选择选项自动添加。 i mean, when the user clicked the button 3 times, so there are 3 input select options:
我的意思是,当用户单击按钮3次时,有3个输入选择选项:
it was working when i have using with this javascript code: 当我使用此javascript代码时,它正在工作:
var counter = 1;
var limit = 30;
function addInput(divName){
var kdprov= $("#provinsi_id").val();
if (counter == limit) {
alert("Maksimal input ialah " + counter + " sekolah");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "<div><span class='input-group-addon' ><b> Sekolah ke-"+ (counter + 1)+"</span></b><select class='form-control' name='npsn"+
(counter + 1)+"' ><option value='' disabled selected>--Pilih Sekolah--</option>'.<?php foreach($sekolah as $sekolah1){ ?>'<option value='<?php echo $sekolah1->npsn;?>' ><?php echo $sekolah1->nama_sekolah; ?></option><?php } ?>.'</select></select></div>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
And this html code: 而这个html代码:
<input type="button" value="Tambah Sekolah" onClick="addInput('sekolah');"> </br></br>
but, the problem. 但是,问题。 they are retrieved all of variable
$sekolah
in database because i only used SELECT * FROM Sekolah
in sql code. 他们在数据库中检索了变量
$sekolah
所有变量,因为我仅在sql代码中使用SELECT * FROM Sekolah
。
And now I want to made a query SELECT * FROM Sekolah where provinsi=$provinsi
in my select option. 现在我要查询
SELECT * FROM Sekolah where provinsi=$provinsi
我的选择选项SELECT * FROM Sekolah where provinsi=$provinsi
。 but, the other problem is variable $provinsi
got from javascricpt value. 但是,另一个问题是从javascricpt值获得的变量
$provinsi
。 I mean,it is a chained dropdown. 我的意思是,这是一个链接的下拉列表。 when i choose a option from
$provinsi
, so i want #sekolah
option will retrieve only from the selected $provinsi
option. 当我从
$provinsi
选择一个选项时,因此我希望#sekolah
选项将仅从所选的$provinsi
选项中检索。
It worked in only static input, but i can't used it in my dimanic input select option. 它仅在静态输入中起作用,但是我无法在我的dimanic输入选择选项中使用它。
So, what should i do? 所以我该怎么做? I'm not really understand about javascript, so can someone give me the solution?
我对javascript不太了解,所以有人可以给我解决方案吗? i made it in codeIgniter.
我是在codeIgniter中取得的。
can i use a query in php code to get data from database in javascript? 我可以使用php代码中的查询从JavaScript中的数据库中获取数据吗?
Here is an example of how I would do this. 这是我将如何执行此操作的示例。 I would create the select menu on load, so there is always one available.
我会在加载时创建选择菜单,因此始终有一个可用菜单。 Then I would clone that select element whenever the button is clicked.
然后,只要单击按钮,我就会克隆该选择元素。 See the example below.
请参见下面的示例。
Working example: https://jsfiddle.net/tkfc031h/1 工作示例: https : //jsfiddle.net/tkfc031h/1
HTML 的HTML
<div id="theSelectElements">
<label class="foo">Number <span>1</span>
<select>
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>
</label>
</div>
<button id="addOne">One more</button>
jQuery jQuery的
$('#addOne').click(function(){
var theSelect = $('.foo:last-child').clone();
var count = (parseInt(theSelect.find('span').text()) + 1);
theSelect.find('span').html(count);
theSelect.find('select').attr('name', "select"+count)
$('#theSelectElements').append(theSelect);
})
First of all, let me clarify what you want. 首先,让我澄清一下您想要什么。
You have a dropdown "Provinsi" contains for example: 您有一个下拉列表“ Provinsi”,例如:
And you have a button "Tambah Sekolah". 然后您有一个按钮“ Tambah Sekolah”。
You select "Sumatera Utara" then click "Tambah Sekolah", you will have a new dropdown "Sekolah", contains for example: 选择“ Sumatera Utara”,然后单击“ Tambah Sekolah”,您将有一个新的下拉列表“ Sekolah”,例如:
You select "Riau & Kepri" then click "Tambah Sekolah", you will have another new dropdown "Sekolah", contains for example: 选择“ Riau&Kepri”,然后单击“ Tambah Sekolah”,您将有另一个新的下拉列表“ Sekolah”,例如:
If the above requirements is what you want, then you should use ajax too. 如果上面的要求是您想要的,那么您也应该使用ajax。 Here what you need to do:
这里您需要做的是:
In this example: https://jsfiddle.net/o0s1L4k1/ , see what happen if "Tambah Sekolah" button is clicked: 在此示例中: https ://jsfiddle.net/o0s1L4k1/,看看如果单击“ Tambah Sekolah”按钮会发生什么:
$.ajax({
method: 'POST',
url: 'list-sekolah.php', // page that will query schools by province
data: {
provinsi: $('#provinsi').val() // send currently selected province
},
success: function(response) {
// build the new <select> and append it
var html = $('<p>Sekolah: <select>' + response + '</select></p>');
$('body').append(html);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.