duanchun1852 2014-12-31 01:54
浏览 43
已采纳

如何同时应用add()和remove()方法?

I require 2 dropdown list name it Parent n child. I have made by child a hidden field to only appear as parent value is clicked.

Im trying to apply both add and remove method concurrently. Here is an example from w3schools on the respective add() and remove() method.

How do i integrate them both in one?

//Add() method

<!DOCTYPE html>
<html>
<body>

<form>
  <select id="mySelect" size="8">
    <option>Apple</option>
    <option>Pear</option>
    <option>Banana</option>
    <option>Orange</option>
  </select>
</form>
<br>

<p>Click the button to add a "Kiwi" option at the end of the dropdown list.</p>

<button type="button" onclick="myFunction()">Insert option</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    var option = document.createElement("option");
    option.text = "Kiwi";
    x.add(option);
}
</script>

</body>
</html>

//remove() method
<!DOCTYPE html>
<html>
<body>

<form>
Select a fruit:
<br>
<select id="mySelect" size="4">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>
</form>
<br>

<button onclick="myFunction()">Remove selected fruit</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    x.remove(x.selectedIndex);
}
</script>

</body>
</html>

Here is the websites to test the codes:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_select_add

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_select_remove

  • 写回答

1条回答 默认 最新

  • douan0729 2015-01-30 03:46
    关注
    <!DOCTYPE html>
    <html>
    <body>
    
    <form>
      <select id="mySelect" size="8">
        <option>Apple</option>
        <option>Pear</option>
        <option>Banana</option>
        <option>Orange</option>
      </select>
    </form>
    <br>
    
    <p>Click the button to add a "Kiwi" option at the end of the dropdown list.</p>
    
    <button type="button" onclick="add()">Insert option</button>
    <button onclick="rem()">Remove selected fruit</button>
    
    <script>
    function add() {
        var x = document.getElementById("mySelect");
        var option = document.createElement("option");
        option.text = "Kiwi";
        x.add(option);
    }
    
    function rem() {
        var x = document.getElementById("mySelect");
        x.remove(x.selectedIndex);
    }
    </script>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错