如何在jQuery中更改按钮的文本值?目前,我的按钮有“添加”作为其文本值,点击后,我希望它改变为“保存”。我尝试过下面这个方法,但到目前为止还没有成功:

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

$("#btnAddProfile").click(function(){
    $("#btnAddProfile").attr('value', 'Save');
 });

使用.val ()

这里有一个到jsf的链接

这取决于你使用的按钮类型

<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来获得更具体的答案。

编辑:当然,假设你用.click()调用包装了它,这些就可以工作了

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

编辑3:如果你使用jQuery UI,你需要使用DaveUK的方法(如下)调整文本属性

document.getElementById('btnAddProfile').value='Save';

你给你的按钮一个类而不是一个id?试试下面的代码

$(".btnSave").attr('value', 'Save');

完全正确,这对我来说很管用;

    $('#btnAddProfile').bind('click',function(){
    $(this).attr('value','save');
})

你确定Jquery是可用的,你的代码是在正确的地方?

要更改按钮的文本,只需执行下面的jQuery行 为

<输入类型=“按钮”值=“XYZ”id=“btnaddprofil”>

use

$("#btnAddProfile").val('Save');

而对于

<button id='btnAddProfile'>XYZ</button>

使用这个

$(“#btnAddProfile”).html(“保存”);

如果你有按钮,这似乎工作:

<button id="button">First caption</button>

$('#button').button();//make it nice
var text="new caption";
$('#button').children().first().html(text);
$("#btnviewdetails").click(function(){
    if($(this).val()!="hide details"){
        $("#detailedoutput").show();
        $(this).val('hide details');
    }else{
        $("#detailedoutput").hide();
        $(this).val('view more details');
    }

});

你可以使用

$("#btnAddProfile").text('Save');

虽然

$("#btnAddProfile").html('Save');

也可以,但这是一种误导(它给人的印象是你可以写什么

$("#btnAddProfile").html('Save <b>now</b>');

当然你不能

对于在jQuery中使用.Button()创建的按钮........

而其他答案将改变文本,他们将混乱的样式的按钮,事实证明,当一个jQuery按钮被渲染的文本是嵌套在一个span例如。

<button id="thebutton">
  <span class="ui-button-text">My Text</span>
</button>

如果您删除了span并将其替换为文本(如其他示例中所示)—您将失去span和相关的格式。

所以你实际上需要改变SPAN标签内的文本,而不是BUTTON!

$("#thebutton span").text("My NEW Text");

或者(如果像我一样是在点击事件中完成的)

$("span", this).text("My NEW Text");

使用JQuery修改按钮文本的正确方法是使用.button()方法:

$( ".selector" ).button( "option", "label", "new text" );

你需要执行。button("refresh")

HTML:

<button id='btnviewdetails' type='button'>SET</button>

JS :

$('#btnviewdetails').text('Save').button("refresh");

这应该可以达到目的:

$("#btnAddProfile").prop('value', 'Save');       
$("#btnAddProfile").button('refresh');
$("#btnAddProfile").html('Save').button('refresh');

这是我的工作 html:

<button type="button" class="btn btn-primary" id="btnSaveSchedule">abc</button>

js

$("#btnSaveSchedule").text("new value");
    $( "#btnAddProfile" ).on( "click",function(event){
    $( event.target ).html( "Save" );
});

$(文档)时函数(){ $(":按钮”).click(函数(){ $ (p) .toggle (); if (this.value=="Add") this。value = "保存"; 其他的。value = "添加"; }); }); <!DOCTYPE html > < html > < >头 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js " > < /脚本> < / >头 <身体> <input type='button' value='Add' id='btnAddProfile'> <p>这是一个内容很少的段落 <p>这是另一小段 < /身体> < / html >

$(“#btnAddProfile”).text(“保存”);

你可以这样使用:

$('#your-button').text('New Value');

你也可以添加额外的属性,像这样:

$(this).text('New Value').attr('disabled', true).addClass('bt-hud').unbind('click');

在c#中更改按钮礼仪的名称。

<button type="submit" name="add" id="btnUpdate" class="btn btn-primary" runat="server" onserverclick="btnUpdate_Click">
Add

btnUpdate.**InnerText** = "Update";