[英]Get checkbox checked value
How do I retrieve checkboxes value individually for redirection of the page.如何单独检索复选框值以重定向页面。 I want to be able to redirect user after they've checked 2 checkboxes;我希望能够在他们选中 2 个复选框后重定向用户; then automatically redirect with it's value as a payload.然后使用它的值作为有效负载自动重定向。
<script>
$("input:checkbox").on('click', function () {
var $box = $(this);
if ($box.is(":checked")) {
var group = "input:checkbox[name='" + $box.attr("name") + "']";
$(group).prop("checked", false);
$box.prop("checked", true);
var1 = $("input:checkbox[name='fooby[1][]']")
var2 = $("input:checkbox[name='fooby[2][]']")
if (($(var1).is(":checked")) & ($(var2).is(":checked"))) {
window.location.href = url + 'var1=' + var1.value + '?var2=' + var2.value
}
} else {
$box.prop("checked", false);
}
});
</script>
I'm trying to redirect user with two payloads - I can't mix var2 with var1 either;我正在尝试使用两个有效负载重定向用户 - 我也不能将 var2 与 var1 混合; it needs to be constant, their values should be sorted accordingly.它需要是恒定的,它们的值应该相应地排序。 When I do this.value it only gives me ONE value which is the last checked value.当我执行 this.value 时,它只给我一个值,即最后一次检查的值。 How can I get 2 different values?如何获得 2 个不同的值?
This is var1 for instance这是 var1 例如
<center><input style="display:block"type="checkbox" id="group1" class="radio" value="SINGLE_EXAM" name="fooby[1][]" />
and this is var2 for instance这是 var2 例如
<center><input type="checkbox" class="radio" value="Long Beach" id="group4" name="fooby[2][]" /> <label for="group4">dTLA</label>
I'm trying to get 2 different values;我正在尝试获得 2 个不同的值; I tried adding everything to an array;我尝试将所有内容添加到数组中; it didnt work out since var1 could be var2 if I do that.它没有解决,因为如果我这样做,var1 可能是 var2。 How do I fix this?我该如何解决?
First of all, you should have shown a working example using code snippet (where people could reproduce the issue) so that there would have been better clarity about what you are trying to achieve.首先,您应该使用代码片段(人们可以在其中重现问题)显示一个工作示例,以便更好地了解您要实现的目标。
Now, the main problem you are facing is because you are using var1.value
and var2.value
.现在,您面临的主要问题是因为您使用的是var1.value
和var2.value
。 But those variables are JQ objects.但这些变量是 JQ 对象。 So you should be using .val()
.所以你应该使用.val()
。
Then, you should have selected only :checked
checkboxes in var1
and var2
that would give you the correct value of selected option.然后,您应该只选择var1
和var2
中的:checked
复选框,这将为您提供所选选项的正确值。
Btw, you are using url + 'var1=' + var1.value + '?var2=' + var2.value
.顺便说一句,您正在使用url + 'var1=' + var1.value + '?var2=' + var2.value
。
Do you mean to use url + '?var1=' + var1.value + '&var2=' + var2.value
你的意思是使用url + '?var1=' + var1.value + '&var2=' + var2.value
As you have not shown a working example, I'm taking liberty to assume some HTML as shown below.由于您没有展示一个工作示例,我冒昧地假设一些 HTML 如下所示。
$("input:checkbox").on('click', function () { var url = 'http://example.com/'; var $box = $(this); if ($box.is(":checked")) { var group = "input:checkbox[name='" + $box.attr("name") + "']"; $(group).prop("checked", false); $box.prop("checked", true); var1 = $("input:checkbox[name='fooby[1][]']:checked") var2 = $("input:checkbox[name='fooby[2][]']:checked") if (($(var1).is(":checked")) & ($(var2).is(":checked"))) { console.log(url + 'var1=' + var1.val() + '?var2=' + var2.val()); // window.location.href = url + 'var1=' + var1.value + '?var2=' + var2.value } } else { $box.prop("checked", false); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="group1" class="radio" value="SINGLE_EXAM" name="fooby[1][]" /> <label for="group1">Single Exam</label> <input type="checkbox" id="group2" class="radio" value="MULTIPLE_EXAM" name="fooby[1][]" /> <label for="group2">Multiple Exams</label> <input type="checkbox" id="group3" class="radio" value="NO_EXAM" name="fooby[1][]" /> <label for="group3">No Exam</label> <br> <input type="checkbox" class="radio" value="Long Beach" id="group4" name="fooby[2][]" /> <label for="group4">dTLA</label> <input type="checkbox" class="radio" value="Medium Beach" id="group5" name="fooby[2][]" /> <label for="group5">eTLA</label> <input type="checkbox" class="radio" value="Short Beach" id="group6" name="fooby[2][]" /> <label for="group6">fTLA</label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.