我想切換到div內容使用JavaScript,而不是jQuery的可見性。我可以使用一個巨大的其他工作,但是我正在嘗試使用for循環。切換視圖的DIV使用javascript數組和單選按鈕
HTML
<fieldset >
<legend>Please indicate below how you intend to pay any pick-related charges for you shipments.</legend>
<div >
<input id="repl_id" name="repl_name" type="radio" value="111">
<label for="repl_id" >Bill to your Account Number.</label>
</div>
<div >
<input id="repl_id" name="repl_name" type="radio" value="222">
<label for="repl_id" >Bill to a paymentcard.</label>
</div>
<div >
<input id="repl_id" name="repl_name" type="radio" value="333">
<label for="repl_id" >Bill to the reciever or a third party.</label>
</div>
<div >
<input id="repl_id" name="repl_name" type="radio" value="444">
<label for="repl_id" >Pay with check or money order.</label>
</div>
<div >
<input id="repl_id" name="repl_name" type="radio" value="555">
<label for="repl_id" >Pay with cash.</label>
</div>
</fieldset>
<div id="111"><p>This is 111</p></div>
<div id="222"><p>This is 222</p></div>
<div id="333"><p>This is 333</p></div>
<div id="444"><p>This is 444</p></div>
<div id="555"><p>This is 555</p></div>
的Javascript
$(":input[type='radio']").on("change", function() {
var choice =[111,222,333,444,555];
if ($(this).prop("checked")) {
for(i=0; i<=choice.length; i++){
if($(this).val() === choice[i]){
$("#"+i).show();
} else {
$("#"+i).hide();
}
}
}
我遇到的問題是,它似乎並沒有認識到我循環遍歷數組的索引。所以它總是讀錯。請勿使用JQuery。
這實際上是jQuery的:P – nicael
標識** **必須是唯一的。如果您需要複製,請使用類。 – j08691
你嘗試過使用無類比較器嗎? ==而不是===?可能類型在某種程度上是不同的。 – MaxOvrdrv