如何在jQuery中改变一个按钮的文本值?目前,我的按钮有'Add'作为它的文本值,在被点击后,我想把它改为'Save'。我已经尝试了下面这个方法,但到目前为止还没有成功。
$("#btnAddProfile").attr('value', 'Save');
取决于你使用的是什么类型的按钮
<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'的方法(如下)来调整文本属性
在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");
。
$("#thebutton span").text("My NEW Text");
或(如果像我一样在点击事件上完成)
$("span", this).text("My NEW Text");
你需要做.button("刷新")
。
HTML :
<button id='btnviewdetails' type='button'>SET</button>
*JS:
$('#btnviewdetails').text('Save').button("refresh");
你可以用这样的方法。
$('#your-button').text('New Value');
你也可以像这样添加额外的属性。
$(this).text('New Value').attr('disabled', true).addClass('bt-hud').unbind('click');
<!--开始片段。 js hide: false console: true babel.false --> -- begin snippet: js hide: false console: true false -->
$(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>
<!--结束片段-->
改变C#中按钮礼仪的名称。
<button type="submit" name="add" id="btnUpdate" class="btn btn-primary" runat="server" onserverclick="btnUpdate_Click">
Add
btnUpdate.**InnerText** = "Update";
$( "#btnAddProfile" ).on( "click",function(event){
$( event.target ).html( "Save" );
});