首页 文章

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

提问于
浏览
447

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

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

21 回答

  • 0

    在C#中更改按钮礼仪的名称 .

    <button type="submit" name="add" id="btnUpdate" class="btn btn-primary" runat="server" onserverclick="btnUpdate_Click">
    Add
    
    btnUpdate.**InnerText** = "Update";
    
  • 76
    $("#btnviewdetails").click(function(){
        if($(this).val()!="hide details"){
            $("#detailedoutput").show();
            $(this).val('hide details');
        }else{
            $("#detailedoutput").hide();
            $(this).val('view more details');
        }
    
    });
    
  • 5
    $( "#btnAddProfile" ).on( "click",function(event){
        $( event.target ).html( "Save" );
    });
    
  • 741

    取决于您使用的按钮类型

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

    EDIT :当然,假设你把它包裹在一个 .click() 的电话中,这些都可行

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

    EDIT 3 :如果你're using jQuery UI, you need to use DaveUK'方法(below)调整文本属性

  • 12

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

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

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

    如果删除 Span 并用文本替换它(如在其他示例中那样) - 您将松开 Span 和相关格式 .

    所以你实际上需要更改SPAN标签内的文本而不是按钮!

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

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

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

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

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

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

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

    使用

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

    而为

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

    用这个

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

  • 1

    你需要做 .button("refresh")

    HTML

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

    JS

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

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

    <button id="button">First caption</button>
    
    $('#button').button();//make it nice
    var text="new caption";
    $('#button').children().first().html(text);
    
  • 4

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

  • 6

    您可以使用

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

    虽然

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

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

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

    但当然你不能

  • 2

    你可以使用这样的东西:

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

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

    $(this).text('New Value').attr('disabled', true).addClass('bt-hud').unbind('click');
    
  • 2
    document.getElementById('btnAddProfile').value='Save';
    
  • 1
    $("#btnAddProfile").click(function(){
        $("#btnAddProfile").attr('value', 'Save');
     });
    
  • 8

    这对我来说很有用:

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

    JS

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

    你有没有给你的按钮而不是id?尝试以下代码

    $(".btnSave").attr('value', 'Save');
    
  • 9
    $(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>
    
  • 18

    这应该是诀窍:

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

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

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

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

相关问题