最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - check if checkbox is checked add value else if unchecked remove value - Stack Overflow

programmeradmin2浏览0评论

I have checkbox 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

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? I normally do for anchors when there is single double quotes.

I have checkbox 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

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? I normally do for anchors when there is single double quotes.

Share Improve this question asked Oct 10, 2013 at 8:30 M Shahzad KhanM Shahzad Khan 9351 gold badge10 silver badges22 bronze badges 2
  • 1 Why don't you use value="1" ? And where is your javascript ? – Brewal Commented Oct 10, 2013 at 8:32
  • check this stackoverflow./questions/19290772/… – Shakti Patel Commented Oct 10, 2013 at 8:48
Add a ment  | 

6 Answers 6

Reset to default 2

Live Demo

You should use value in each checkbox. for ex:

HTML:

<input type="checkbox" value="1" checked />
<input type="checkbox" value="2" checked />
<input type="checkbox" value="3" />

JQuery:

var searchIDs = $("input:checkbox:checked").map(function(){
        return this.value;
    }).toArray();

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

 var selectedvalue=[];
    $(":checkbox:checked").each(function(){

    selectedvalue.push($(this).attr("data"));

    });

alert(selectedvalue.join("-"));// your desired result

demo

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? If you want to use data-* attributes, you should specify an identifier. Something like <input type="checkbox" data-id="1">, then you can get the value using jQuery data() method: $(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.

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);
}
});

Demo Fiddle

javascript

getElementById("chkbox1").checked=true;

jquery

$("#chkbox1").prop("checked");

or

$("#chkbox1").attr("checked");

check this Jsfiddle

$("#btn").click(function(event){
    var selectedvalue=[];
$(":checkbox:checked").each(function(){

selectedvalue.push($(this).attr("data"));

});

alert(selectedvalue.join("-"));// your desired result

  })
发布评论

评论列表(0)

  1. 暂无评论