1 回答
TA贡献1830条经验 获得超9个赞
只需myFunction()
从内部 调用selectChange()
:
<html>
<select id="sel" onchange='myFunction()'>
<option value="car">1</option>
<option value="bike">2</option>
<option value="cycle">3</option>
</select>
<button onclick='selectChange()'>Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
function selectChange() {
document.getElementById('sel').value = "bike";
myFunction();
}
</script>
</html>
但是请注意,您还应该使用Unobstructive JavaScript,并使用事件侦听器而不是onchangeand onclick:
document.getElementById('sel').addEventListener('change', myFunction);
document.getElementsByTagName('button')[0].addEventListener('click', selectChange);
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
function selectChange() {
document.getElementById('sel').value = "bike";
myFunction();
}
<select id="sel">
<option value="car">1</option>
<option value="bike">2</option>
<option value="cycle">3</option>
</select>
<button>Click me</button>
<p id="demo"></p>
添加回答
举报