Mr07QAQ
Maximus2333
采纳率100%
2021-02-25 02:29

怎么在互换select的同时把内容里的option hidden同时互换

50

大佬们,我想在按下swap按钮的时候不光互换select中选中内容,同时互换select中隐藏起来的option

代码如下

<TR class="inputa"> 
	<TH><b></b></TH>
<TD>
<input class="country" type="text" id="select1" value="China" readonly="readonly">
<INPUT name=input onchange="convert(this.form,form.measure1, form.measure2)"  placeholder="input" height="48" style="position: relative; left: 0px; font-size: 20px;">
</TD>
<TD> 
<SELECT name=measure1 
 onchange="convert(this.form,this, form.measure2)" style="position: absolute; left: 600px; font-size: 20px;">

 <OPTION selected value=0.001>ml

    <OPTION value=1>l
    
    <OPTION hidden value=0.473>pint
    
    <OPTION  hidden value=3.785>gallon
    
    <OPTION hidden value=0.0295>ounce
    
</OPTION>
</SELECT>
</TD>
</TR>




<TR class="inputb"> 
	<TH ><b></b></TH>
<TD> 
<input class="country" type="text" id="select2" value="UK" readonly="readonly">
<INPUT name=display placeholder="output" height="48" style="position: relative; left: 0px; font-size: 20px;" >
</TD>
<TD> 
<SELECT name=measure2 
 onchange="convert(this.form,form.measure1, this)" style="position: absolute; left: 600px; font-size: 20px;">
 
 <OPTION hidden value=0.001>ml

    <OPTION hidden value=1>l
    
    <OPTION selected value=0.473>pint
    
    <OPTION value=3.785>gallon
    
    <OPTION value=0.0295>ounce

</OPTION>    
</SELECT>
</TD>
</TR>
<TR>
    <TD>
    <input type="button" onclick="swap(this.form);" value="^swap^" class="swapbutton"/>
    
    <script type="text/javascript">
    function swap(form) {
        var t = form.measure1.value;
        form.measure1.value = form.measure2.value;
        form.measure2.value = t;
        convert(form,form.measure1, form.measure2)

        var aa=document.getElementById('select1').value
        var bb=document.getElementById('select2').value
        document.getElementById('select1').value=bb
        document.getElementById('select2').value=aa
    }
    </script>
    </TD>
</TR>
</TBODY> 
</TABLE>

想要实现的效果如下:

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • jslang 天际的海浪 2月前
        <script type="text/javascript">
        function swap(form) {
            for (var i = 0; i < form.measure1.options.length; i++) {
                var t = form.measure1.options[i].hidden;
                form.measure1.options[i].hidden = form.measure2.options[i].hidden;
                form.measure2.options[i].hidden = t;
            }
            var t = form.measure1.value;
            form.measure1.value = form.measure2.value;
            form.measure2.value = t;
            convert(form,form.measure1, form.measure2)
     
            var aa=document.getElementById('select1').value
            var bb=document.getElementById('select2').value
            document.getElementById('select1').value=bb
            document.getElementById('select2').value=aa
        }
        </script>
    
    点赞 1 评论 复制链接分享
  • Mr07QAQ Maximus2333 2月前

    想实现的效果:

    点赞 评论 复制链接分享