[英]Filter dropdown options by selected option value
I'm trying to make a dropdown option filter by another selected options value on dropdwon change event.我正在尝试通过 dropdwon 更改事件上的另一个选定选项值进行下拉选项过滤器。
I'm trying do with this jquery but nothing happens.我正在尝试使用此 jquery 但没有任何反应。
$(function() {
var options = $("#selectAmount").html();
$("#currencyList").change(function(e) {
var text = $("#selectLang :selected").val();
$("#selectAmount").html(options);
if (text == "Select Amount") return;
$('#selectAmount :not([value^="' + text.substr(0, 3) + '"])').remove();
});
});
And The HTML + PHP和 HTML + PHP
<select class="custom-select" id="selectLang">
<option selected disabled>Select Your Country </option>
<?php foreach($countries as $country): ?>
<option value="<?php echo $country['country_id']; ?> ">
<?php echo $country['country_name']; ?> </option>
<?php endforeach; ?>
</select>
<select name="currency" id="currencyList" class="custom-select">
<option selected disabled>Select Currency </option>
<?php foreach($currencies as $currency): ?>
<option value="<?php echo $currency['currency_id']; ?> ">
<?php echo $currency['currency_name']; ?> </option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="amount">Custom Amount</label>
<div class="input-group">
<div class="input-group-prepend"> <span class="input-group-text">$</span>
</div>
<a href="#" data-target="#view-plans" data-toggle="modal" class="view-plans-link">View Plans</a>
<select class="custom-select" id="selectAmount">
<option>Select Amount</option>
</select>
And The Full Jquery which I used for filtering以及我用于过滤的完整 Jquery
$(document).ready(function() {
// ------------------ [ Country dropdown Change Event ] --------------
$("#selectLang").change(function() {
var countryId = $(this).val();
$("#selectFrame").fadeIn('slow');
$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
langId: countryId
},
dataType: "JSON",
success: function(result) {
var items = "";
$("#selectFrame").empty();
$("#selectCode").empty();
$("#selectFrame").append(
"<option selected disabled> Select Operator </option>");
$("#selectCode").append(
"<input type='text' class='form-control form-control-md' id='selectCode' value=''>");
$.each(result, function(i, item) {
$("#selectFrame").append("<option value='" + item
.operator_id + "'>" + item.operator_name +
"</option>");
});
$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
framId: countryId
},
dataType: 'JSON',
success: function(result) {
var code = "";
$("#selectCode").empty();
$.each(result, function(i, code) {
$('#selectCode').val(code.code);
});
$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
cuId: countryId
},
dataType: 'JSON',
success: function(result) {
var amount = "";
$("#selectAmount").empty();
$.each(result, function(i, amount) {
$("#selectAmount").append("<option value='" + amount
.country_id + "'>" + amount.amount_name +
"</option>");
});
}
});
}
});
}
});
});
$("#currencyList").change(function() {
var currencyId = $(this).val();
$(this).fadeIn();$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
amId: currencyId
},
dataType: 'JSON',
success: function(result) {
var amount = "";
$("#selectAmount").empty();
$.each(result, function(i, amount) {
$("#selectAmount").append("<option>" + amount.amount_name +
"</option>");
});
}
});
});
$(function() {
var options = $("#selectAmount").html();
$("#currencyList").change(function(e) {
var text = $("#selectLang :selected").val();
$("#selectAmount").html(options);
if (text == "Select Amount") return;
$('#selectAmount :not([value^="' + text.substr(0, 3) + '"])').remove();
});
});
});
Everything Works fine but when I select currency dropdown, The amount dropdown resets and shows all the value by currency id from database.一切正常,但是当我 select 货币下拉菜单时,金额下拉菜单会重置并按数据库中的货币 ID 显示所有值。
Screenshot 1 Screenshot 2截图 1截图 2
So, Now I exactly wanting to do removing the extra dropdowns by filtering with the value of Country dropdown.所以,现在我确实想通过过滤 Country 下拉列表的值来删除额外的下拉列表。
Please kindly help to complete this...请帮助完成此...
Edit: Again I tried this simple script编辑:我再次尝试了这个简单的脚本
$("#currencyList").change(function() { var sel = $("#currencyList:selected").val(); $("#selectAmount option[value.= '"+ sel + "' ]");remove(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="currencyList"> <option>Select Currency</option> <option value="1"> Bitcoin </option> <option value="2"> Ethereum </option> <option value="3"> Litecoin </option> </select> <select id="selectAmount"> <option>Select Amount</option> <option value="1"> 0.0001 BTC </option> <option value="2"> 0.005 ETH </option> <option value="3"> 0.5 LTC </option> </select>
Now this Code works like a charm but if II use this script to现在这个代码就像一个魅力,但如果我使用这个脚本
$("#currencyList").change(function() {
var sel = $("#selectLang :selected").val();
$("#selectAmount option[value!= '"+ sel + "' ]").remove();
});
track the change of #currencyList and filter by the selected option value of #selectLang the code don't works....跟踪#currencyList 的变化并通过#selectLang 的选定选项值过滤代码不起作用....
Have any solution please...?请问有什么解决办法吗...?
I have solved the problem.我已经解决了这个问题。 The mistake was in the jquery is that I had used filter query in the below of OnChange function错误在 jquery 中是我在 OnChange function 下面使用了过滤器查询
$("#currencyList").change(function() {
var currencyId = $(this).val();
$(this).fadeIn();$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
amId: currencyId
},
dataType: 'JSON',
success: function(result) {
var amount = "";
$("#selectAmount").empty();
$.each(result, function(i, amount) {
$("#selectAmount").append("<option>" + amount.amount_name +
"</option>");
});
}
});
});
$(function() {
var sel = $("#selectLang").find(':selected').val();
$("#selectAmount option[value!= '"+ sel + "' ]").remove();
});
});
});
Recently, I have understood the actual problem and fixed it by adding the the filter query into the Onchange SUCCESS result function... Like This -最近,我了解了实际问题并通过将过滤器查询添加到 Onchange SUCCESS 结果 function 中来修复它......就像这样 -
$("#currencyList").change(function() {
var currencyId = $(this).val();
$(this).fadeIn();
$.ajax({
url: 'vendor/filter/process-controller.php',
type: 'POST',
data: {
amId: currencyId
},
dataType: 'JSON',
success: function(result) {
var amount = "";
$("#selectAmount").empty();
$.each(result, function(i, amount) {
$("#selectAmount").append("<option value='" + amount
.country_id + "'>" + amount.amount_name +
"</option>");
});
var sel = $("#selectLang").find(':selected').val();
$("#selectAmount option[value!= '"+ sel + "' ]").remove();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.