我有多个复选框,并尝试仅检查复选框,但在应用jquery时,我无法访问其范围外的值。这样做的原因是我要传递ajax请求并且没有做任何事情。
我的代码html:
<div name="state" id="state">
<label class="checkbox-inline">
<input type="checkbox" id="offer1" name="offer" value="Monday">Monday
</label>
<label class="checkbox-inline">
<input type="checkbox" id="offer2" name="offer" value="Tuesday">Tuesday
</label>
<label class="checkbox-inline">
<input type="checkbox" id="offer3" name="offer" value="Wednesday">Wednesday
</label>
<label class="checkbox-inline">
<input type="checkbox" id="offer4" name="offer" value="Thursday">Thursday
</label>
<label class="checkbox-inline">
<input type="checkbox" id="offer5" name="offer" value="Friday">Friday
</label>
</div>
<button name="submit" onclick="continued()" type="submit" class="btn btn-primary btn-block" style="font-size:18px;">Continue</button>
<script>
function continued(){
var s=$("#school").val();
var st=$("#student").val();
var selected = [];
var sThisVal;
var v =$('#state input:checkbox').each(function (elem) {
sThisVal = (this.checked ? $(this).val() : "");
//alert(sThisVal);
return sThisVal;
});
alert(v[0]);
}
</script>
答案 0 :(得分:1)
netstandard15
没有返回类型。您应该在.each()
处使用.map()
功能:
.get()
答案 1 :(得分:1)
function continued() {
var selected = [];
$("#state input[type='checkbox']:checked").each(function() {
selected.push(this.value);
});
console.log(selected);
return selected;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="state">
<label class="checkbox-inline">
<input type="checkbox" value="Monday">Monday
</label>
<label class="checkbox-inline">
<input type="checkbox" value="Tuesday">Tuesday
</label>
<label class="checkbox-inline">
<input type="checkbox" value="Wednesday">Wednesday
</label>
<label class="checkbox-inline">
<input type="checkbox" value="Thursday">Thursday
</label>
<label class="checkbox-inline">
<input type="checkbox" value="Friday">Friday
</label>
</div>
<button onclick="continued()">Continue</button>