如何编辑该代码,以默认情况下禁用select2和select3并在上一个选择上存在选择或者选择上包含选项(“选择您的选项占位符”除外)的情况下启用它。
在此代码上,当选择上一个选择时,最后一个选项也被选中,相反,我想选择第一个选项占位符,而不是“选择您的选项占位符”
下面是我的代码
<select name="select1" id="select1">
<option value="" disabled selected>Select your option</option>
<option value="1" option-id="1">Group A</option>
<option value="2" option-id="2">Group B</option>
<option value="3" option-id="3">Group C</option>
</select>
<select name="select2" id="select2">
<option value="a" disabled selected option-id="a">Select your option</option>
<option value="a" option-id="a">Select your option</option>
<option value="1" option-id="1">Product 1 No Sizes</option>
<option value="2" option-id="1">Product 2 Standard and large</option>
<option value="3" option-id="2">Product 3 Small and Standard</option>
<option value="4" option-id="2">Product 4 Standard and Large</option>
<option value="5" option-id="3">Product 5 No Sizes</option>
</select>
<select name="select3" id="select3">
<option value="a" disabled selected option-id="a">Select your option</option>
<option value="bb" option-id="1" >Standard</option>
<option value="bb" option-id="2" >Standard</option>
<option value="cc" option-id="2" >Large</option>
<option value="aa" option-id="3" >Small</option>
<option value="bb" option-id="3" >Standard</option>
<option value="bb" option-id="4" >Standard</option>
<option value="cc" option-id="4" >Large</option>
<option value="cc" option-id="4" >Large</option>
<option value="cc" option-id="4" >Large</option>
</select>
var $select1 = $( '#select1' ),
$select2 = $( '#select2' ),
$select3 = $( '#select3' ), // I added that line but not sure if its correct
$options_a = $select2.find( 'option' ),
$options_b = $select3.find( 'option' ); // I added that line but not sure if its correct
$select1.on( 'change', function() {
$select2.html( $options_a.filter( '[option-id="' + this.value + '"]' ) );
} ).trigger( 'change' );
// I added the next lines for select3 but not sure if they are correct
$select2.on( 'change', function() {
$select3.html( $options_b.filter( '[option-id="' + this.value + '"]' ) );
} ).trigger( 'change' );
https://jsfiddle.net/arabtornado/up738s1x/27/
我正在寻找的是这样的东西 https://www.jqueryscript.net/demo/Multilevel-Dependent-Dropdown-Plugin-With-jQuery-Dependent-Dropdowns/
其中select2和3被禁用,并且即使在启用占位符之后,默认情况下也会选择占位符,而不是选择中的最后一个选项,因此用户将被迫选择一个选项。
谢谢
答案 0 :(得分:1)
尝试一下:
$("#select2").attr("disabled","");
$("#select3").attr("disabled","");
$(document).on("change","#select1",function (event) {
if($("#select1")[0].selectedIndex == 0){
$("#select2").attr("disabled","");
$("#select3").attr("disabled","");
$("#select2")[0].selectedIndex = 0;
$("#select3")[0].selectedIndex = 0;
}else{
$("#select2").removeAttr("disabled");
$("#select3").removeAttr("disabled");
}
});
<select name="" id="select1">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="" id="select2">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="" id="select3">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<script src="jquery-3.3.1.min.js"></script>
答案 1 :(得分:1)
如果我正确理解您的要求。这应该做的工作。您不需要那个CSS。
<select name="select1" id="select1">
<option value="" disabled selected>Select your option</option>
<option value="1" option-id="1">Group A</option>
<option value="2" option-id="2">Group B</option>
<option value="3" option-id="3">Group C</option>
</select>
<select name="select2" id="select2" disabled>
<option value="a" disabled selected option-id="a">Select your option</option>
<option value="1" option-id="1">Product 1 No Sizes</option>
<option value="2" option-id="1">Product 2 Standard and large</option>
<option value="3" option-id="2">Product 3 Small and Standard</option>
<option value="4" option-id="2">Product 4 Standard and Large</option>
<option value="5" option-id="3">Product 5 No Sizes</option>
</select>
<select name="select3" id="select3" disabled>
<option value="a" disabled selected option-id="a">Select your option</option>
<option value="bb" option-id="1" >Standard</option>
<option value="bb" option-id="2" >Standard</option>
<option value="cc" option-id="2" >Large</option>
<option value="aa" option-id="3" >Small</option>
<option value="bb" option-id="3" >Standard</option>
<option value="bb" option-id="4" >Standard</option>
<option value="cc" option-id="4" >Large</option>
<option value="cc" option-id="4" >Large</option>
<option value="cc" option-id="4" >Large</option>
</select>
var $select1 = $( '#select1' ),
$select2 = $( '#select2' ),
$select3 = $( '#select3' ), // I added that line but not sure if its correct
$options_a = $select2.find( 'option' ),
$options_b = $select3.find( 'option' ); // I added that line but not sure if its correct
$select1.on( 'change', function() {
$select2.prop("disabled", false);
$select2.html( $options_a.filter(function () {
return $(this).attr('option-id') === $select1.val() ||
$(this).attr('option-id') === "a"
}))
$select2.val('a')
} )
// I added the next lines for select3 but not sure if they are correct
$select2.on( 'change', function() {
$select3.prop("disabled", false);
$select3.html( $options_b.filter(function () {
return $(this).attr('option-id') === $select2.val() ||
$(this).attr('option-id') === "a"
}));
$select3.val('a')
} )