如何在jQuery中更改按钮的文本?

时间:2011-04-07 11:56:04

标签: jquery jquery-ui

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

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

21 个答案:

答案 0 :(得分:807)

取决于您使用的按钮类型

<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版本(来自&gt; 1.6)使用.prop而不是.attr

编辑3 :如果您使用的是jQuery UI,则需要使用调整文本属性的DaveUK方法(below

答案 1 :(得分:137)

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

虽然其他答案会改变文本,但它们会弄乱按钮的样式,但事实证明,当呈现jQuery按钮时,按钮的文本嵌套在一个范围内,例如

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

如果删除范围并将其替换为文本(如其他示例所示) - 您将松开范围和相关格式。

因此,您实际上需要更改SPAN标记内的文本而不是BUTTON!

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

或(如果像我一样,它是在点击事件上完成的)

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

答案 2 :(得分:80)

如果使用JQuery“按钮化”按钮文本,更改按钮文本的正确方法是使用.button()方法:

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

答案 3 :(得分:34)

要更改按钮的文本,只需执行以下jQuery行 对

<input type='button' value='XYZ' id='btnAddProfile'>

使用

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

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

使用此

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

答案 4 :(得分:32)

答案 5 :(得分:19)

您需要.button("refresh")

HTML

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

JS

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

答案 6 :(得分:12)

如果您按下了按钮,这似乎有效:

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

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

答案 7 :(得分:9)

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

答案 8 :(得分:9)

您可以使用以下内容:

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

您还可以添加以下额外属性:

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

答案 9 :(得分:8)

您可以使用

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

虽然

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

也会起作用,但这会产生误导性(它给人的印象是你可以写出像

这样的东西
$("#btnAddProfile").html('Save <b>now</b>');

但当然你不能

答案 10 :(得分:6)

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

答案 11 :(得分:5)

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

答案 12 :(得分:4)

它为我工作 HTML:

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

JS

$("#btnSaveSchedule").text("new value");

答案 13 :(得分:2)

您是否为按钮提供了课程而不是ID?尝试以下代码

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

答案 14 :(得分:2)

$(document).ready(function(){
    $(":button").click(function(){
        $("p").toggle();

    if (this.value=="Add") this.value = "Save";
    else this.value = "Add";

  });
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>

<input type='button' value='Add' id='btnAddProfile'>

<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>

</body>
</html>

答案 15 :(得分:1)

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

});

答案 16 :(得分:1)

这应该可以解决问题:

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

答案 17 :(得分:1)

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

答案 18 :(得分:0)

这是完全正确的,这对我有用;

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

你确定Jquery可用并且你的代码在正确的位置吗?

答案 19 :(得分:0)

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

答案 20 :(得分:0)

在C#中更改按钮礼节的名称。

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

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