[英]check if checkbox is checked add value else if unchecked remove value
I have checkbox html. 我有复选框html。
<label><input type="checkbox"><span>Air Conditioning</span></label>
<label><input type="checkbox"><span>Cable TV Service</span></label>
<label><input type="checkbox"><span>Private Bathroom</span></label>
I need to have seperate value for each checkbox, so I can get those values from all and store in variable. 我需要为每个复选框设置单独的值,以便可以从所有值中获取这些值并将其存储在变量中。 For example 例如
<label><input type="checkbox" data="1"><span>Air Conditioning</span></label>
<label><input type="checkbox" data="2"><span>Cable TV Service</span></label>
<label><input type="checkbox" data="3"><span>Private Bathroom</span></label>
So if all the 3 are selected I will need my variable as 因此,如果全部选择了3个,我将需要我的变量作为
room_features = '1-2-3';
If any one is then unchecked, variable will be updated as 如果未选中任何一项,则变量将更新为
room_features = '2-3';
So on every change on checkbox, variable should be updated. 因此,对复选框的每次更改,都应更新变量。 I am confused if either adding data="1" etc is fine for checkboxes? 我是否为复选框添加data =“ 1”等是否感到困惑? I normally do for anchors when there is single double quotes. 当有单双引号时,我通常会做锚。
You should use value in each checkbox. 您应该在每个复选框中使用值 。 for ex: 例如:
HTML: HTML:
<input type="checkbox" value="1" checked />
<input type="checkbox" value="2" checked />
<input type="checkbox" value="3" />
JQuery: jQuery的:
var searchIDs = $("input:checkbox:checked").map(function(){
return this.value;
}).toArray();
html html
<label><input type="checkbox" checked="checked" data="1"><span>Air Conditioning</span></label>
<label><input type="checkbox" checked="checked" data="2"><span>Cable TV Service</span></label>
<label><input type="checkbox" data="3"><span>Private Bathroom</span></label>
JS JS
var selectedvalue=[];
$(":checkbox:checked").each(function(){
selectedvalue.push($(this).attr("data"));
});
alert(selectedvalue.join("-"));// your desired result
reference Arrays and checked-selector 参考数组和选中的选择器
You can use .map()
method, note that I have added value
attribute to the element instead of data
as an input should have a value otherwise what's the point of using it? 您可以使用.map()
方法,请注意,我已经向元素添加了value
属性而不是data
因为输入应具有值,否则使用它有什么意义? If you want to use data-*
attributes, you should specify an identifier. 如果要使用data-*
属性,则应指定一个标识符。 Something like <input type="checkbox" data-id="1">
, then you can get the value using jQuery data()
method: $(elem).data('id');
类似于<input type="checkbox" data-id="1">
,然后可以使用jQuery data()
方法获取值: $(elem).data('id');
. 。
<label><input type="checkbox" value="1"><span>Air Conditioning</span></label>
...
var vals = $('input[type=checkbox]:checked').map(function(){
return this.value; // return $(this).data('whatever');
}).get().join('-');
get()
returns an array, if you need an array you can remove the .join()
method which returns a string. get()
返回一个数组,如果需要一个数组,则可以删除返回字符串的.join()
方法。
javascript javascript
getElementById("chkbox1").checked=true;
jquery jQuery的
$("#chkbox1").prop("checked");
or 要么
$("#chkbox1").attr("checked");
try this dynamic one 试试这个动态的
var room_features = "";
$('[type=checkbox]').change(function () {
var data = "-" + $(this).attr('data');
if (room_features.indexOf(data) != -1) {
room_features = room_features.replace(data, "");
alert(room_features);
} else {
room_features = room_features + data;
alert(room_features);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.