[英]dynamic dependent select box.. set to certain value
我正在處理我在這里找到的這個腳本: https://www.webslesson.info/2020/04/dynamic-dependent-select-box-with-search-option-in-php-using-jquery-ajax。 html工作正常。 我想為類別項目設置一個特定的值..通常你會用選項值設置它..我看到這是腳本的一部分..所以例如..在數據庫中,它存儲我們有城市和國家。 所以一旦我 select 類別國家 - >英格蘭,然后它會自動將所有城市(包括倫敦)列為子類別。 如果稍后您想修改連接到英格蘭 - 倫敦的用戶記錄.. 通過調出此腳本,您將不得不再次 select 國家和城市.. 但我希望它是 state 英格蘭 - > 倫敦,然后我可以 select,布萊頓而不是倫敦。
<div class="form-group">
<label>Select Category</label>
<select name="category_item" id="category_item" class="form-control input-lg" data-live-search="true" title="Select Category"></select>
</div>
<div class="form-group">
<label>Select Sub Category</label>
<select name="sub_category_item" id="sub_category_item" class="form-control input-lg" data-live-search="true" title="Select Sub Category"></select>
</div>
<script>
$(document).ready(function(){
$('#category_item').selectpicker();
$('#sub_category_item').selectpicker();
load_data('category_data');
function load_data(type, category_id = '')
{
$.ajax({
url:"load_data.php",
method:"POST",
data:{type:type, category_id:category_id},
dataType:"json",
success:function(data)
{
var html = '';
for(var count = 0; count < data.length; count++)
{
html += '<option value="'+data[count].id+'">'+data[count].name+'</option>';
}
if(type == 'category_data')
{
$('#category_item').html(html);
$('#category_item').selectpicker('refresh');
}
else
{
$('#sub_category_item').html(html);
$('#sub_category_item').selectpicker('refresh');
}
}
})
}
$(document).on('change', '#category_item', function(){
var category_id = $('#category_item').val();
load_data('sub_category_data', category_id);
});
});
</script>
您將需要選項標簽的 id 或值。 在您的情況下是data[count].id 。 因此,要設置您將使用的值:
$('#selector_id').value = data[count].id; // Id or the value of the option you want to set
編輯:
在您的特定情況下,這將像這樣工作:
$('#container_id').value = country_id; //2 for example for England
$('#sub_container_id').value = city_id; //21 for example
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.