如何更改 jQuery 中按钮的文本? [英] How to change the text of a button in jQuery?

查看:31
本文介绍了如何更改 jQuery 中按钮的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 jQuery 中更改按钮的文本值?目前,我的按钮的文本值是添加",单击后我希望将其更改为保存".我在下面尝试过这种方法,但到目前为止没有成功:

How do you change the text value of a button in jQuery? Currently, my button has 'Add' as its text value, and upon being clicked I want it to change to 'Save'. I have tried this method below, but so far without success:

$("#btnAddProfile").attr('value', 'Save');

推荐答案

取决于你使用的按钮类型

Depends on what type of button you are using

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").attr('value', 'Save'); //versions older than 1.6

<input type='button' value='Add' id='btnAddProfile'>
$("#btnAddProfile").prop('value', 'Save'); //versions newer than 1.6

<!-- Different button types-->

<button id='btnAddProfile' type='button'>Add</button>
$("#btnAddProfile").html('Save');

您的按钮也可以是链接.您需要发布一些 HTML 以获得更具体的答案.

Your button could also be a link. You'll need to post some HTML for a more specific answer.

EDIT :当然,假设您已将其包装在 .click() 调用中,这些将起作用

EDIT : These will work assuming you've wrapped it in a .click() call, of course

EDIT 2 :较新的 jQuery 版本(来自 > 1.6)使用 .prop 而不是 .attr

EDIT 2 : Newer jQuery versions (from > 1.6) use .prop rather than .attr

EDIT 3:如果您使用的是 jQuery UI,则需要使用 DaveUK 的方法(下面) 调整文本属性

EDIT 3 : If you're using jQuery UI, you need to use DaveUK's method (below) of adjusting the text property

这篇关于如何更改 jQuery 中按钮的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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