通过检查收音机创建文本
Create text by checking radios
我想通过检查收音机(最后不按按钮)在 contentediable 中创建一些文本。我创建了这个但不明白为什么它不起作用:/
我猜函数的参数有问题,但我不知道为什么。
谢谢你的帮助:)
function genereItem(x){
document.getElementById("item"+x).innerHTML = document.querySelector("input[name="+x+"]:checked").value;
}
.editable{
border: solid black 1px;
}
<div>
<div>itemA1
<input id="yesA1" name="A1" type="radio" value="item A1 = yes," onchange="genereItem("A1")"/>
<label for="yesA1">yes</label>
<input id="noA1" name="A1" type="radio" value="item A1 = no," onchange="genereItem("A1")"/>
<label for="noA1">no</label>
<input id="NTA1" name="A1" type="radio" value="item A1 = NT," checked="checked" onchange="genereItem("A1")"/>
<label for="NTA1">NT</label>
</div>
<div>itemA2
<input id="yesA2" name="A2" type="radio" value="item A2 = yes." onchange="genereItem("A2")"/>
<label for="yesA2">yes</label>
<input id="noA2" name="A2" type="radio" value="item A2 = no." onchange="genereItem("A2")"/>
<label for="noA2">no</label>
<input id="NTA2" name="A2" type="radio" value="item A2 = NT." checked="checked" onchange="genereItem("A2")"/>
<label for="NTA2">NT</label>
</div>
<div>itemB1
<input id="yesB1" name="B1" type="radio" value="item B1 = yes," onchange="genereItem("B1")"/>
<label for="yesB1">yes</label>
<input id="noB1" name="B1" type="radio" value="item B1 = no," onchange="genereItem("B1")"/>
<label for="noB1">no</label>
<input id="NTB1" name="B1" type="radio" value="item B1 = NT," checked="checked" onchange="genereItem("B1")"/>
<label for="NTB1">NT</label>
</div>
<div>itemB2
<input id="yesB2" name="B2" type="radio" value="item B2 = yes." onchange="genereItem(B2)"/>
<label for="yesB2">yes</label>
<input id="noB2" name="B2" type="radio" value="item B2 = no." onchange="genereItem(B2)"/>
<label for="noB2">no</label>
<input id="NTB2" name="B2" type="radio" value="item B2 = NT." checked="checked" onchange="genereItem("B2")"/>
<label for="NTB2">NT</label>
</div>
<div class="editable" contenteditable="true">
<div>
<string id="itemA1"></string>
<string id="itemA2"></string>
<p/>
</div>
<div>
<string id="itemB1"></string>
<string id="itemB2"></string>
<p/>
</div>
</div>
</div>
genereItem(A1)
Uncaught ReferenceError: A1 is not defined
使用genereItem("A1")
(字符串,不是变量名)
我想通过检查收音机(最后不按按钮)在 contentediable 中创建一些文本。我创建了这个但不明白为什么它不起作用:/
我猜函数的参数有问题,但我不知道为什么。
谢谢你的帮助:)
function genereItem(x){
document.getElementById("item"+x).innerHTML = document.querySelector("input[name="+x+"]:checked").value;
}
.editable{
border: solid black 1px;
}
<div>
<div>itemA1
<input id="yesA1" name="A1" type="radio" value="item A1 = yes," onchange="genereItem("A1")"/>
<label for="yesA1">yes</label>
<input id="noA1" name="A1" type="radio" value="item A1 = no," onchange="genereItem("A1")"/>
<label for="noA1">no</label>
<input id="NTA1" name="A1" type="radio" value="item A1 = NT," checked="checked" onchange="genereItem("A1")"/>
<label for="NTA1">NT</label>
</div>
<div>itemA2
<input id="yesA2" name="A2" type="radio" value="item A2 = yes." onchange="genereItem("A2")"/>
<label for="yesA2">yes</label>
<input id="noA2" name="A2" type="radio" value="item A2 = no." onchange="genereItem("A2")"/>
<label for="noA2">no</label>
<input id="NTA2" name="A2" type="radio" value="item A2 = NT." checked="checked" onchange="genereItem("A2")"/>
<label for="NTA2">NT</label>
</div>
<div>itemB1
<input id="yesB1" name="B1" type="radio" value="item B1 = yes," onchange="genereItem("B1")"/>
<label for="yesB1">yes</label>
<input id="noB1" name="B1" type="radio" value="item B1 = no," onchange="genereItem("B1")"/>
<label for="noB1">no</label>
<input id="NTB1" name="B1" type="radio" value="item B1 = NT," checked="checked" onchange="genereItem("B1")"/>
<label for="NTB1">NT</label>
</div>
<div>itemB2
<input id="yesB2" name="B2" type="radio" value="item B2 = yes." onchange="genereItem(B2)"/>
<label for="yesB2">yes</label>
<input id="noB2" name="B2" type="radio" value="item B2 = no." onchange="genereItem(B2)"/>
<label for="noB2">no</label>
<input id="NTB2" name="B2" type="radio" value="item B2 = NT." checked="checked" onchange="genereItem("B2")"/>
<label for="NTB2">NT</label>
</div>
<div class="editable" contenteditable="true">
<div>
<string id="itemA1"></string>
<string id="itemA2"></string>
<p/>
</div>
<div>
<string id="itemB1"></string>
<string id="itemB2"></string>
<p/>
</div>
</div>
</div>
genereItem(A1)
Uncaught ReferenceError: A1 is not defined
使用genereItem("A1")
(字符串,不是变量名)