[英]PHP and JS - How to get value from chosen checkbox is checked and enter?
我只想创建动态值,该值将根据已选中的复选框发送到警报弹出窗口。
1 -这当复选框测试1被选中 , 然后按回车键,就警戒值平均值=“1”
2- 选中“测试1”,“测试2”和“测试3” 并按回车键后 ,给出的值=“ 1、2、3”,方法相同。
3-然后,如果选中了所有复选框, 然后按Enter ,它将给出所有值=“ 1,2,3,4”
我已经开始使用此功能,但我无法考虑如何编写功能。
function checkTest(){
// script and condition here that i'm stuck to begin
alert(values);
}
<input type="checkbox" id="st_1" class="cbgroup1" name="cbg1[]" value="1"> Test 1<br/>
<input type="checkbox" id="st_2" class="cbgroup1" name="cbg1[]" value="2"> Test 2<br/>
<input type="checkbox" id="st_3" class="cbgroup1" name="cbg1[]" value="3"> Test 3<br/>
<input type="checkbox" id="st_4" class="cbgroup1" name="cbg1[]" value="4"> Test 4<br/>
<input type="checkbox" id="cbgroup1_master" onchange="togglecheckboxes(this,'cbgroup1')"> Select All <br/>
<button type="button" onclick="javascript:checkTest();" class="btn btn-default"> Enter </button>
编辑正确。 不得不取出$,因为它们不起作用并且不希望进行故障排除。 这将完全按照您的要求工作。 虽然只有jQuery迭代。 需要使用Java脚本更改某些方面。
function checkTest(){
// script and condition here that i'm stuck to begin
var checkboxes = jQuery('.cbgroup1'), values = '';
checkboxes.each(function(){
if(jQuery(this).is(':checked') || jQuery('#cbgroup1_master').is(':checked')){
if(values != ''){
values += ',';
}
values += jQuery(this).val();
}
});
alert(values);
}
您可以使用以下代码检查该复选框是否已选中:
function checkTest(){
// script and condition here that i'm stuck to begin
var c1 = document.getElementById("st_1").checked;
var c2 = document.getElementById("st_2").checked;
var c3 = document.getElementById("st_3").checked;
var c4 = document.getElementById("st_4").checked;
var values = "";
if(c1){
//first check box is checked. you can add your desired code here
values = values + "1";
}
if(c2){
values = values + ",2";
}
if(c3){
values = values + ",3";
}
if(c4){
values = values + ",4";
}
alert(values);
}
或者,如果我误解了您的问题:
function checkTest(){
// script and condition here that i'm stuck to begin
var c1 = document.getElementById("st_1").checked;
var c2 = document.getElementById("st_2").checked;
var c3 = document.getElementById("st_3").checked;
var c4 = document.getElementById("st_4").checked;
var values = "";
if(c1){
//first check box is checked. you can add your desired code here
values = values + "1";
}
if(c1 && c2){
values = values + ",2";
}
if(c1 && c2 && c3){
values = values + ",3";
}
if(c1 && c2 && c3 && c4){
values = values + ",4";
}
alert(values);
}
编辑:与jQuery:
function checkTest(){
// script and condition here that i'm stuck to begin
var c1 = $("#st_1").prop("checked");
var c2 = $("#st_2").prop("checked");
var c3 = $("#st_3").prop("checked");
var c4 = $("#st_4").prop("checked");
var values = "";
if(c1){
//first check box is checked. you can add your desired code here
values = values + "1";
}
if(c1 && c2){
values = values + ",2";
}
if(c1 && c2 && c3){
values = values + ",3";
}
if(c1 && c2 && c3 && c4){
values = values + ",4";
}
alert(values);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.