Javascript 尝试将对象作为innerHTML函数的参数传入
我试图根据几个不同对象的选择,用信息填充多个表格单元格innerHTML。我目前有多个功能,每个选项一个:Javascript 尝试将对象作为innerHTML函数的参数传入,javascript,function,object,innerhtml,Javascript,Function,Object,Innerhtml,我试图根据几个不同对象的选择,用信息填充多个表格单元格innerHTML。我目前有多个功能,每个选项一个: <input onClick="firstChoice();" type="radio" id="first-choice"> <input onClick="secondChoice();" type="radio" id="second-choice"> <tr> <td>A</td> <td id="aCel
<input onClick="firstChoice();" type="radio" id="first-choice">
<input onClick="secondChoice();" type="radio" id="second-choice">
<tr>
<td>A</td>
<td id="aCell">xx</td>
</tr>
<tr>
<td>B</td>
<td id="bCell">xx</td>
</tr>
<script>
function Foo(a, b) {
this.a = a;
this.b = b;
}
first = new Foo(42, 8);
second = new Foo(100, 4);
var aCell = document.getElementById("first-choice");
var bCell = document.getElementById("second-choice");
function firstChoice() {
aCell.innerHTML = first.a;
bCell.innerHTML = first.b;
}
function secondChoice() {
aCell.innerHTML = second.a;
bCell.innerHTML = second.b;
}
</script>
或者可能有一种完全不同的方式来完成任务——任何帮助都将不胜感激 您在正确的轨道上:
function choice(choicePicked) {
aCell.innerHTML = choicePicked.a;
bCell.innerHTML = choicePicked.b;
}
您只需更改事件处理程序,如下所示:
<input onClick="choice(first);" type="radio" id="first-choice">
<input onClick="choice(second);" type="radio" id="second-choice">
这样,您将所需的对象传递给选择函数。您是否确实尝试过该函数ChoicePicked{?因为这正是您所需要的。感谢您的回复!抱歉,回复太晚,问题解决时我忘了接受您的答案。
<input onClick="choice(first);" type="radio" id="first-choice">
<input onClick="choice(second);" type="radio" id="second-choice">