无法在 select 框中的 javascript 中执行两个操作
Can't able to perform two actions in a javascript on select box
无法执行两个操作(即,我需要禁用并需要取消select select 中的 selected 选项)
- 我有两个 select 盒子,即 select_one 盒子是 ('1','2','3') & select_two 盒子是 ('please select' , 'test1', 'test2','test3'...等等)
- 并且我在 select_one 框上写了 onchange
- 当我从 select_box 下拉菜单中 select 编辑选项 1 或 3 时,我需要从 select_two 框中取消 select 当前值并需要显示 'please select' 选项,我还需要禁用 select_two 框。
const checkFun = ( value ) => {
if( value == '1' || value == '3' )
{
document.getElementById("select_two").disabled = true;
document.getElementById("select_two").options[0].selected = "selected";
} else
{
document.getElementById("select_two").disabled= false
}
}
Select One: <select name="testOne" id="select_one" onchange="checkFun( this.value )"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>
Select Two: <select name="testTwo" id="select_two"><option value=""> - please select - </option><option value="test1">test1</option><option value="test2">test2</option><option value="test3">test3</option></select>
谢谢,
KK
将 value 属性添加到 - 请 select - 选项标签如 value="0"
<select id="select_one">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="select_two">
<option value="0"> - please select - </option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>
<script>
const boxTwo = document.getElementById('select_two');
document.getElementById('select_one').addEventListener('change',function(){
boxTwo.value = 0;
if (this.value == 2) {
boxTwo.disabled = false;
}else{
boxTwo.disabled = true;
}
},{passive:true});
</script>
一切正常!
const checkFun = (value) => {
if (value == '1' || value == '3') {
document.getElementById("select_two").disabled = true;
document.getElementById("select_two").options[0].selected = true;
} else {
document.getElementById("select_two").disabled = false
}
}
<select name="test1" id="select_two">
<option value=""> - please select -</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>
<select name="test" id="select_one" onchange="checkFun( this.value )">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
function checkFun(value) {
const s2 = document.getElementById("select_two");
s2.value = "";
s2.disabled = (value == '1' || value == '3');
}
<select name="test" id="select_one" onchange="checkFun( this.value )">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="test1" id="select_two" disabled>
<option value=""> - please select -</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>
无法执行两个操作(即,我需要禁用并需要取消select select 中的 selected 选项)
- 我有两个 select 盒子,即 select_one 盒子是 ('1','2','3') & select_two 盒子是 ('please select' , 'test1', 'test2','test3'...等等)
- 并且我在 select_one 框上写了 onchange
- 当我从 select_box 下拉菜单中 select 编辑选项 1 或 3 时,我需要从 select_two 框中取消 select 当前值并需要显示 'please select' 选项,我还需要禁用 select_two 框。
const checkFun = ( value ) => {
if( value == '1' || value == '3' )
{
document.getElementById("select_two").disabled = true;
document.getElementById("select_two").options[0].selected = "selected";
} else
{
document.getElementById("select_two").disabled= false
}
}
Select One: <select name="testOne" id="select_one" onchange="checkFun( this.value )"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>
Select Two: <select name="testTwo" id="select_two"><option value=""> - please select - </option><option value="test1">test1</option><option value="test2">test2</option><option value="test3">test3</option></select>
谢谢, KK
将 value 属性添加到 - 请 select - 选项标签如 value="0"
<select id="select_one">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="select_two">
<option value="0"> - please select - </option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>
<script>
const boxTwo = document.getElementById('select_two');
document.getElementById('select_one').addEventListener('change',function(){
boxTwo.value = 0;
if (this.value == 2) {
boxTwo.disabled = false;
}else{
boxTwo.disabled = true;
}
},{passive:true});
</script>
一切正常!
const checkFun = (value) => {
if (value == '1' || value == '3') {
document.getElementById("select_two").disabled = true;
document.getElementById("select_two").options[0].selected = true;
} else {
document.getElementById("select_two").disabled = false
}
}
<select name="test1" id="select_two">
<option value=""> - please select -</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>
<select name="test" id="select_one" onchange="checkFun( this.value )">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
function checkFun(value) {
const s2 = document.getElementById("select_two");
s2.value = "";
s2.disabled = (value == '1' || value == '3');
}
<select name="test" id="select_one" onchange="checkFun( this.value )">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="test1" id="select_two" disabled>
<option value=""> - please select -</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
</select>