我有一些radiobutton 選項
<input checked="checked" id="PositionA_A001" name="PositionA" type="radio" value="A001"><label for="PositionA_A001">a</label>
<input id="PositionA_A002" name="PositionA" type="radio" value="A002">
<label for="PositionA_A002">b</label>
<input id="PositionA_A003" name="PositionA" type="radio" value="A003">
<label for="PositionA_A003">c</label>
<input id="PositionA_A004" name="PositionA" type="radio" value="A004">
<label for="PositionA_A004">d</label>
<input id="PositionA_A005" name="PositionA" type="radio" value="A005">
<label for="PositionA_A005">e</label>
<div class="html-helper-divsub" id="PositionA_A005_Block" style="display: none;"><input class="form-control" id="PositionOtherA" name="PositionOtherA" type="text" value=""></div>
我需要取他的值做一些字串相加的處理
let PositionA = $("input[name='PositionA']:checked").val();
這邊console.log還是顯示他的value,可是我一但這樣做就會顯示[object HTMLDivElement]
let string = PostitionA + ....+...;
請問這是為什麼呢?我用其他的radiobutton都沒有此問題
$("input[name='PositionA']:checked")
是選取到所有checked
的
你應該在後面加上index
例:$("input[name='PositionA']:checked")[0]
然後再加.val()
例:$("input[name='PositionA']:checked")[0].val()
不好意思請教一下
一個容器裡面的radio不是只能單選?
還是我搞錯了甚麼
對,只能單選,但$("input[name='PositionA']:checked")
沒有說是他是radio,checkbox也能用這個,所以要附上index
附上code
<input checked="checked" id="PositionA_A001" name="PositionA" type="radio" value="A001"><label for="PositionA_A001">a</label>
<input id="PositionA_A002" name="PositionA" type="radio" value="A002">
<label for="PositionA_A002">b</label>
<input id="PositionA_A003" name="PositionA" type="radio" value="A003">
<label for="PositionA_A003">c</label>
<input id="PositionA_A004" name="PositionA" type="radio" value="A004">
<label for="PositionA_A004">d</label>
<input id="PositionA_A005" name="PositionA" type="radio" value="A005">
<label for="PositionA_A005">e</label>
<div class="html-helper-divsub" id="PositionA_A005_Block" style="display: none;"><input class="form-control" id="PositionOtherA" name="PositionOtherA" type="text" value=""></div>
<button onclick="log()">log</button>
<script>
function log() {
alert(
$("input[name='PositionA']:checked")[0].value
);
}
</script>
Html
<input checked="checked" id="PositionA_A001" name="PositionA" type="radio" value="A001"><label for="PositionA_A001">a</label>
<input id="PositionA_A002" name="PositionA" type="radio" value="A002">
<label for="PositionA_A002">b</label>
<input id="PositionA_A003" name="PositionA" type="radio" value="A003">
<label for="PositionA_A003">c</label>
<input id="PositionA_A004" name="PositionA" type="radio" value="A004">
<label for="PositionA_A004">d</label>
<input id="PositionA_A005" name="PositionA" type="radio" value="A005">
<label for="PositionA_A005">e</label>
<div class="html-helper-divsub" id="PositionA_A005_Block" style="display: none;"><input class="form-control" id="PositionOtherA" name="PositionOtherA" type="text" value=""></div>
<br>
<input type="button" value="Test" onclick="Test()">
<p id="TestValue"></p>
Javascript
function Test()
{
let PositionA = $("input[name='PositionA']:checked").val();
document.getElementById("TestValue").innerHTML=PositionA;
}
Demo
資料輸出時可能缺了 innerHTML