Valor de atributo de enlace de datos en jquery
Frecuentes
Visto 23,000 equipos
0
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 }" />
Así es como lo intenté
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
3 Respuestas
4
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">
contestado el 28 de mayo de 14 a las 12:05
2
You have incorrect syntax to get data attribute. You need to use:
$(this).data('bind')
contestado el 28 de mayo de 14 a las 12:05
Actually, the given syntax works as well. If you call data
with no arguments, you get back an object with the information stored on it as properties. This is about three-quarters of the way down the page you linked. Example: jsbin.com/zafakobe/1 - TJ Crowder
im getting the message in the messagebox as data-bind="attr: { value: Id } - Hamish
And in fact, the question was buried at the end of the question: The OP is getting the value of data-bind
, just not the value they expected to get. - TJ Crowder
You saved my day. Thanks. - Mohamed Saqib
2
puedes hacerlo de esta manera:
var value = $(input).data("bind");
o de esta manera:
var value =$(input).attr("data-bind");
contestado el 28 de mayo de 14 a las 12:05
True, but it doesn't answer the question of why what the OP's doing (which is valid) doesn't work. - TJ Crowder
im getting the message in the messagebox as data-bind="attr: { value: Id } - Hamish
Quiere decir data().bind()
in valid? - Ehsan Sajjad
@EhsanSajjad: Yes, .data().bind
(no ()
) es válido. - TJ Crowder
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas jquery html knockout.js or haz tu propia pregunta.
Tenga en cuenta que después
var input = $(this)
,input
is ya haya utilizado a jQuery instance. No need to then repeat the$()
haciendo$(input)
. It's harmless, but pointless. - T.J. Crowder