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

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

当前回答

$("#btnviewdetails").click(function(){
    if($(this).val()!="hide details"){
        $("#detailedoutput").show();
        $(this).val('hide details');
    }else{
        $("#detailedoutput").hide();
        $(this).val('view more details');
    }

});

其他回答

对于在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");
document.getElementById('btnAddProfile').value='Save';
    $( "#btnAddProfile" ).on( "click",function(event){
    $( event.target ).html( "Save" );
});

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

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

$('#button').button();//make it nice
var text="new caption";
$('#button').children().first().html(text);

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