简体   繁体   English

JavaScript onchange事件不会更改div内容

[英]JavaScript onchange event doesn't change div content

I want to replace the content of my <div id="score_here">10</div> , when I select a value from my drop-down <select id="ansRate" onchange="javascript:add_score(this);"> . 当我从下拉菜单<select id="ansRate" onchange="javascript:add_score(this);">选择一个值时,我想替换<div id="score_here">10</div> <select id="ansRate" onchange="javascript:add_score(this);"> I have setup my JavaScript function which is shown below, but it doesn't work. 我已经设置了我的JavaScript函数,如下所示,但是它不起作用。 What is the problem with this? 这是什么问题?

<div>
<select id="ansRate" onchange="javascript:add_score(this);">
<option value="0">Answer Rate</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>

<div id="score_here" style="width:100px;height:100px;background-color:yellow;">
 81
</div>

<script type="text/javascript">

function add_score(a){
   var string = $("#questionScore").text();
   var thenum = string.replace( /^\D+/g, ''); 
   var add = $(a).val(); 
   var total = parseInt(thenum) + parseInt(add);
   document.getElementByID('score_here').innerHTML = total;
 }

 </script>
document.getElementById("id") 

document.getElementByID("id")

should be: 应该:

document.getElementById('score_here').innerHTML = total;

:-) :-)

Try this: 尝试这个:

document.getElementByID is not the correct syntax, instead try document.getElementById document.getElementByID语法不正确,请尝试使用document.getElementById

You can done everything by jQuery. 您可以使用jQuery完成所有操作。

Just replace this line: 只需替换此行:

document.getElementByID('score_here').innerHTML = total;

to

$('#score_here').html(total);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM