[英]Change the values inside a select tag, when I click a button
I have a form with a select tag that have some options. 我有一个带有选择标记的表格,其中有一些选项。 I want that when I click in the button to change the value in the select tag, and in that way the tag will have the value I define by
我希望当我单击按钮以更改选择标记中的值时,这样标记将具有我定义的值
document.getElementById('fruits').innerHTML="computer"
In this JSFiddle it does not function when I click the button. 在此JSFiddle中 ,当我单击按钮时它不起作用。
As i see jquery you can try: 正如我看到的jQuery,您可以尝试:
HTML: HTML:
<select id="sel">
<option>Apple</option>
<option>Orange</option>
</select>
<input type="button" value="change" onclick="fill()">
JS: JS:
function fill() {
$('#sel').empty();
$("#sel").append(new Option("Computer"));
}
Or with vanilla js you can try: 或者使用香草js,您可以尝试:
function fill(){
document.getElementById('sel').innerHTML="<option>Computer</option>";
}
function fill(){
document.getElementById('fruits').innerHTML="<option>Meat</option>";}
<select id="fruits">
<option> Apple</option>
<option> Orange</option>
</select>
If I am getting you right then make these change in your code. 如果我做对了,请在您的代码中进行这些更改。
Try this 尝试这个
$('#change').click(function() {
document.getElementById('fruits').innerHTML="<option>Meat</option>";
});
modified HTML to 修改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.