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

查看:96
本文介绍了如何在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.

编辑:当然,只要您将其包装在.click()调用中,这些方法就会起作用

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

编辑2 :较新的jQuery版本(从> 1.6开始)使用.prop而不是.attr

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

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

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天全站免登陆