簡體   English   中英

單擊按鈕時,更改選擇標記內的值

[英]Change the values inside a select tag, when I click a button

我有一個帶有選擇標記的表格,其中有一些選項。 我希望當我單擊按鈕以更改選擇標記中的值時,這樣標記將具有我定義的值

document.getElementById('fruits').innerHTML="computer"

在此JSFiddle中 ,當我單擊按鈕時它不起作用。

正如我看到的您可以嘗試:

HTML:

<select id="sel">
    <option>Apple</option>
    <option>Orange</option>
</select>
<input type="button" value="change" onclick="fill()">

JS:

function fill() {
    $('#sel').empty();
    $("#sel").append(new Option("Computer"));

}

演示

或者使用香草js,您可以嘗試:

function fill(){
    document.getElementById('sel').innerHTML="<option>Computer</option>";
}

演示2

function fill(){
    document.getElementById('fruits').innerHTML="<option>Meat</option>";}

<select id="fruits">
    <option> Apple</option>
    <option> Orange</option>
</select>

如果我做對了,請在您的代碼中進行這些更改。

嘗試這個

$('#change').click(function() {
    document.getElementById('fruits').innerHTML="<option>Meat</option>";
});

修改HTML為

<select id="fruits"> 
    <option> Apple</option>
    <option> Orange</option>
</select>

<input type="button" value="change" name="change" id="change">
 your html is wrong. use the code like:

 <select id="fruits">
        <option>Apple</option>
        <option>Orange</option>
  </select>

  <input type="button" value="change" onclick="fill();" />

 And change the fill function to:

  function fill() {
        document.getElementById('fruits').innerHTML = "<option>Meat</option>";
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM