javascript - 如何根据另一个选择下拉列表中的选择从选择下拉列表中删除选项?

标签 javascript jquery drop-down-menu selectize.js

我正在使用 JavaScript 选择下拉列表。我想根据代码中选择的预订类型更改付款类型。 代码如下,

$(document).ready(function () {
    $("#reservationType").change(function () {           
         managePaymentTypes();
    });      
});

var $reservationTypeObj = $('#reservationType').selectize({
    sortField: {
       field: localStorage.getItem("selectizeField"),
       direction: localStorage.getItem("selectizeDirection")
    },
       dropdownParent: localStorage.getItem("selectizeDropdownParent")
});

var $paymentTypeObj = $('#paymentType').selectize({
    sortField: {
       field: localStorage.getItem("selectizeField"),
       direction: localStorage.getItem("selectizeDirection")
    },
        dropdownParent: localStorage.getItem("selectizeDropdownParent")
});

function managePaymentTypes(){
   var resType = $('#reservationType :selected').val();
      if(resType==202){
        //remove option 2 - Salary deduct in selectize.
      }else if(resType==101) {
        //remove option 1 - Credit in selectize. 
      }else{
        ////
      }
}
<label class="col-md-1 control-label firstcol" for="type" style="text-align:left">Reservation Type : </label> 
<div class="col-md-2" id="reservationTypeDiv" data-toggle="tooltip" data-container="body"  data-placement="bottom">
	<select class="demo-default selectized" id="reservationType" name="reservationType" tabindex="7"  opacity: 1" data-toggle="tooltip" data-container="body"  data-placement="bottom">
		<%
		for (int l = 0; l < reservationTypes.size(); l++)
		{
		%>
			<option value="<%=reservationTypes.get(l).getId()%>"><%=reservationTypes.get(l).getDescription()%></option>
		<%
		}
		%>
	</select>    
</div>
<label class="col-md-1 control-label firstcol" for="payment" style="text-align:left">Payment Type : </label> 
<div class="col-md-2" data-toggle="tooltip" id="paymentTypeDiv" data-container="body"  data-placement="bottom">
	<select class="demo-default selectized" id="paymentType"       name="paymentType" tabindex="7" opacity: 1" data-      toggle="tooltip" data-container="body"  data-placement="bottom">
		<%
		for (int l = 0; l < reservationPaymentTypes.size(); l++)
		{
		%>
			<option value="<%=reservationPaymentTypes.get(l).getId()%>"><%=reservationPaymentTypes.get(l).getDescription()%></option>
		<%
		}
		%>
	</select>    
</div>    

我有两个选择下拉菜单。

  1. 对于预订类型
  2. 对于付款类型

值是从数据库获取的。 Reservation types drop-down screenshot , Payment types drop-down screenshot

当预订类型更改时,我想更改付款类型。 因此,在更改预订类型时,我调用了一个名为managePaymentTypes()的方法。当预订类型为个人(101) 时,我想删除信用付款选项。我想在预订类型为公司(202)时删除工资扣除选项。个人应该是默认选择。这是我的要求。有人请帮我解决这个问题。

最佳答案

我想出了以下解决方案。

    $(document).ready(function () {      
<%
    for(SportsCenterReservationPaymentType paymentType : reservationPaymentTypes){
    %>
        paymentTypes.push({id: <%=paymentType.getId()%>, desc: '<%=paymentType.getDescription()%>'});
    <%
    }
    %>
    $("#reservationType").change(function () {           
        loadResourceTable();
        managePaymentTypes();
    });      

});

function managePaymentTypes(){ 
    var resType = $('#reservationType :selected').val();
    $('#paymentType')[0].selectize.clearOptions();

    if(resType==202&& resType!=101){

        $.each(paymentTypes, function (idx, obj) {
            if(obj.id!=333){
              $('#paymentType')[0].selectize.addOption({value:obj.id,text:obj.desc})  
            }
        });
    }

    if(resType==101&&resType!=202){

        $.each(paymentTypes, function (idx, obj) {
            if(obj.id!=222){
              $('#paymentType')[0].selectize.addOption({value:obj.id,text:obj.desc})  
            }
        });
    }

}

关于javascript - 如何根据另一个选择下拉列表中的选择从选择下拉列表中删除选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315498/

相关文章:

javascript - 使用 AngularJS 在 ngclick 上触发函数

Jquery .remove() 不会改变 .length

html - 尝试在嵌套的 <li> 样式菜单中选择 CSS3 元素

javascript - 下拉菜单 li :hover stops working and have to click to open submenu

javascript - 多个内联脚本是否比一个联合的内联脚本慢?

javascript - vite-plugin-pages 出现错误 Cannot find module '~pages' 或其相应的类型声明

javascript - 数据表列中的逗号和 $

javascript - DIV 中文本的垂直滚动(循环)

apache-flex - Flex DropdownList 创建完成错误

javascript - 如何从xhr获取标题?