Reputation: 87
I have a checkboxlist which is being binded from the database and Id of the checkbox is being binded with the help of data-bind attribute and when the user clicks the submit button I'm iterating through the checkboxlist and checking whether the Checkbox is checked on not, if it is checked then i want get the Id of the checkbox.
<input type="checkbox" class='roles' name='roles' data-bind="attr: { value: Id }" />
This is how i tried
if ($(this).is(':checkbox')) {
if (this.checked)
{
var input = $(this);
if ($(input).data().bind) {
alert($(this).data('bind'));
}
}
}
i actually want to get this value data-bind="attr: { value: Id }
But in the alertbox im getting message as data-bind="attr: { value: Id }, where as i want to get 1,2 etc
Upvotes: 0
Views: 24261
Reputation: 1073968
But in the alertbox im getting message as data-bind="attr: { value: Id }, where as i want to get 1,2 etc
Right, the value of data-bind
hasn't changed. If you want the value of the value
attribute, you need to get that instead:
alert(input.val());
var obj = {Id: "foo"};
ko.applyBindings(obj, document.body);
display("data-bind: " + $("input").data().bind);
display("value: " + $("input").val());
Side note: Knockout has a value
binding for setting the value of a form control:
<input data-bind="value: Id">
Upvotes: 4
Reputation: 62488
you can do this way:
var value = $(input).data("bind");
or this way:
var value =$(input).attr("data-bind");
Upvotes: 2
Reputation: 82231
You have incorrect syntax to get data attribute. You need to use:
$(this).data('bind')
Refer to .data() Documentation
Upvotes: 2