[英]How to get attr value in jquery
I am trying to get value of attr " _last_val " from my input but unable to produce it. 我试图从输入中获取attr“_last_val”的值,但无法生成它。
Below is ? 下边是 ? i have tried demo
我试过演示
//below is HTML //下面是HTML
<form method="post" action="" id="feedback_form">
<input type="text" value="2014-08-11" class="date" name="add_by_datetime" _last_val="2014-08-14" >
<input type="button" name="submit_now" value="Submit" />
</form>
// below is script //下面是脚本
jQuery(function($) {
$("form#feedback_form input[name='submit_now']").on("click",function() {
var actualadddate = $("form#feedback_form input[name='add_by_datetime']").attr('_last_val');
alert(+actualadddate+'aaaaaaaaa');
});
});
please let me know where i am wrong. 请让我知道我哪里错了。
Thanks 谢谢
Remove the preceded +
from alert and try, 删除前面的
+
来自警报并尝试,
alert(actualadddate + 'aaaaaaaaa');
Note that, in your example you are using .date class
to access the attribute if your page has more than 1 element having same class then it will not give you the accurate date. 请注意,在您的示例中,如果您的页面具有多个具有相同类的元素,那么您使用
.date class
来访问该属性,那么它将不会为您提供准确的日期。 So, be carefull in that case or use unique id
to get the attribute. 因此,在这种情况下要小心,或使用
unique id
来获取属性。
because of the +
before actualadddate its converted to a number, that results in NaN ( Not a Number
) 因为在actualadddate之前的
+
转换为数字,导致NaN( Not a Number
)
so, remove it 所以,删除它
alert(actualadddate+'aaaaaaaaa');
http://jsfiddle.net/59o60g7e/10/ http://jsfiddle.net/59o60g7e/10/
Since, actualadddate
is not a number, it thows NaN ie not a number. 因为,
actualadddate
不是一个数字,它会使NaN即不是数字。 Remove +
from the alert which you used to typecast. 从用于类型转换的警报中删除
+
。
Use this instead, 改用它,
alert(actualadddate+"aaaaaaaaa");
Also, instead of using user defined attributes. 此外,而不是使用用户定义的属性。 Use
data
attribute to store your custom values. 使用
data
属性存储自定义值。
The only thing wrong in your code is the alert
call. 您的代码中唯一错误的是
alert
调用。 http://jsfiddle.net/rcnw1op0/ http://jsfiddle.net/rcnw1op0/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.