addAttr在jquery中不起作用? [英] addAttr not working in jquery?

查看:129
本文介绍了addAttr在jquery中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码:

<input type="text" name="color" id="color" value="" />

和jquery

$('#color').change(function(){
    $('#color').addAttr('value', 'test');
});

当我在文本框中更改值时,结果为 value =和firebug中的错误 $(#color)。addAttr不是函数

When I change value in textbox is result value="" and error in firebug $("#color").addAttr is not a function

如何修复它?

推荐答案

它被称为 .attr(),而不是 .addAttr()

你还应该替换你的内部 $('#color ')使用 $(this)表示您正在操作正在更改的相同输入:

You should also replace your inner $('#color') with $(this) to indicate that you're manipulating the same input that's being changed:

$('#color').change(function(){
    $(this).attr('value', 'test');
});

这篇关于addAttr在jquery中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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