jquery: cannot fetch the "value" attribute of a div
Here's a screenshot of my chrome javascript console demonstrating my dilemma.
I seriously cannot understand 开发者_StackOverflow社区why i can't fetch the "value" attribute. The "class" attribute works just fine, so i figure the same should apply to "value". The code (coffeescript) i'm testing in my app looks like this:
$ ->
$(".comment").click ->
alert $(this).attr 'value'
Neither this nor the code shown in this pic work.
Does anyone know what i'm doing wrong or what i SHOULD be doing? Thanks in advance!
Use .val()
. This will work only on select, input, textarea
tags. For other tags, value
attribute is not valid.
If you're using latest jQuery, use data-
attirbutes instead:
<div class="comment" data-value="64">
var value = $('[div selector]').data('value');
I suspect that value
is special since its used for the value of form inputs and whatnot, I'm reading the jQuery source now to see exactly what's happening; but if you change it to data-value
as would be proper in the HTML5 spec, that would likely work and would be standard.
use some thing like this
$("div[data-request]").data() , This returns the object if you debug .
精彩评论