【发布时间】:2017-03-10 03:43:48
【问题描述】:
我正在尝试干掉这段代码,而不是在我想要循环的 if/else 语句中手动输入 elems 选项(或增加数字)。我尝试过使用各种循环,但无法让它在 .change() 事件中工作。
var radioSelect = $("input[type='radio']");
var elems = ["q1","q2","q3","q4","q5","q6"];
radioSelect.change(function () {
// grab the name and value selected radio
// set localstorage
var linkName = $(this).attr('name');
var value = $(this).val();
localStorage.setItem(linkName, value);
// I am trying to loop/increment both through the elements
// and also through the #response[x] divs.
if (link_name === elems[1])
{
$("#response1").html(localStorage.getItem(linkName));
}
else if (link_name === elems[2])
{
$("#response2").html(localStorage.getItem(linkName));
}
else if (link_name === elems[3]) {
$("#response3").html(localStorage.getItem(linkName));
}
});
基本 HTML
<input type="radio" name="q2" value="Yes">
<input type="radio" name="q2" value="No">
【问题讨论】:
标签: javascript jquery arrays loops