jQuery $(this).val返回0 [英] jQuery $(this).val returns 0

查看:274
本文介绍了jQuery $(this).val返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的清单,像这样:

I have a simple list like this:

<ul id="large_box_custom_list">
<li id="8" class="large_box">Item 1</li>
<li id="13" class="large_box">Item 2</li>
</ul>

然后我有一个这样的jQuery函数:

then I have a jQuery function like this:

$(function() { 
$('li.large_box').css('cursor', 'pointer')
.click(function() {
    var show_id = $(this).val();
    alert(show_id);
    });
});

当我单击列表项时,当我期望值为8或13时,alery的值为0.

When I click the list items my alery shows a value of 0 when I am expecting a value of 8 or 13.

推荐答案

因为您应该使用标准DOM元素id属性. jQuery的 .val() 方法与元素ID无关.

Because you should be using the standard DOM element id property. jQuery's .val() method has nothing to do with an element's ID.

$('li.large_box').css('cursor', 'pointer').click(function ()
{
    var show_id = this.id;
    alert(show_id);
});

这篇关于jQuery $(this).val返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆