[英]jQuery data-id undefined
I am unsure why I am not getting my data-id
value its being showed as undefined
我不确定为什么我没有得到显示为undefined
data-id
值
The data-id
shows an int data-id
显示一个整数
<li>
<input style="width:100%;" placeholder="Page Title" id="post_title" onclick="urlCheck()" dataid="<?php echo($website_id);?>" name="post_title" type="text" value="<?php echo set_value('post_title', $post['post_title']); ?>" />
<?php echo form_error('post_title'); ?>
</li>
JS: JS:
function urlCheck() {
$(".postForm").on('click', '#post_title', function (e) {
var id = $('#post_title').attr("data-id");
console.log(id);
e.preventDefault();
});
}
The problem is that you are using dataid
instead of data-id
, and to retrieve the attibute you need to use .data()
instead of .attr()
问题是您使用的是dataid
而不是data-id
,要检索dataid
,您需要使用.data()
而不是.attr()
change this: 改变这个:
<input style="width:100%;" placeholder="Page Title" id="post_title" onclick="urlCheck()" dataid="<?php echo($website_id);?>" name="post_title" type="text" value="<?php echo set_value('post_title', $post['post_title']); ?>" />
to this: 对此:
<input style="width:100%;" placeholder="Page Title" id="post_title" onclick="urlCheck()" data-id="<?php echo($website_id);?>" name="post_title" type="text" value="<?php echo set_value('post_title', $post['post_title']); ?>" />
and retrieve the data in this mode: 并在此模式下检索数据:
var id = $('#post_title').data("id");
您的属性是dataid
,而不是data-id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.