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

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

当前回答

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

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

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

其他回答

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

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

HTML:

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

JS :

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

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

<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的方法(如下)调整文本属性

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

});