jQuery에서 버튼의 텍스트를 변경하는 방법은 무엇입니까?
jQuery에서 버튼의 텍스트 값을 어떻게 변경합니까?현재 제 버튼은 텍스트 값으로 '추가'가 있으며, 클릭 시 '저장'으로 변경하기를 원합니다.아래와 같은 방법을 시도해 보았지만 성공하지 못했습니다.
$("#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()물론입니다.
EDIT 2: 최신 jQuery 버전(> 1.6) 사용.prop보다는.attr
EDIT 3: jQuery UI를 사용하는 경우 DaveUK의 텍스트 속성 조정 방법(아래)을 사용해야 합니다.
로 만든 단추의 경우.jQuery의 Button()...
다른 답변은 텍스트를 변경하지만, jQuery 버튼이 렌더링되면 버튼의 텍스트가 범위 내에 중첩되는 것으로 나타났습니다.
<button id="thebutton">
<span class="ui-button-text">My Text</span>
</button>
범위를 제거하고 텍스트로 바꾸면(다른 예제에서처럼) 범위와 관련 서식이 늘어납니다.
따라서 버튼이 아닌 SPAN 태그 내의 텍스트를 실제로 변경해야 합니다!
$("#thebutton span").text("My NEW Text");
또는 (나처럼 클릭 이벤트에서 수행되는 경우)
$("span", this).text("My NEW Text");
JQuery를 사용하여 단추 텍스트를 "버튼화"한 경우 단추 텍스트를 변경하는 올바른 방법은 .button() 방법을 사용하는 것입니다.
$( ".selector" ).button( "option", "label", "new text" );
단추의 텍스트를 변경하려면 다음 줄의 jQuery를 실행합니다.
<input type='button' value='XYZ' id='btnAddProfile'>
사용하다
$("#btnAddProfile").val('Save');
의 기간 동안
<button id='btnAddProfile'>XYZ</button>
이것을 사용합니다.
$("#btnAddProfile").html('Save');
사용하다.val()
당신은 해야 합니다..button("refresh")
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');
$("#btnAddProfile").text("Save");
버튼을 눌렀을 경우 다음과 같이 작동합니다.
<button id="button">First caption</button>
$('#button').button();//make it nice
var text="new caption";
$('#button').children().first().html(text);
사용할 수 있습니다.
$("#btnAddProfile").text('Save');
비록 ~일지라도
$("#btnAddProfile").html('Save');
그것도 효과가 있겠지만, 오해의 소지가 있습니다(그것은 당신이 다음과 같은 것을 쓸 수 있다는 인상을 줍니다).
$("#btnAddProfile").html('Save <b>now</b>');
하지만 물론 당신은 할 수 없습니다.
그건 나를 위한 일입니다 html:
<button type="button" class="btn btn-primary" id="btnSaveSchedule">abc</button>
제이에스
$("#btnSaveSchedule").text("new value");
$("#btnAddProfile").click(function(){
$("#btnAddProfile").attr('value', 'Save');
});
document.getElementById('btnAddProfile').value='Save';
$(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>
당신은 당신의 버튼에 id 대신 클래스를 주었습니까? 다음 코드를 시도하세요.
$(".btnSave").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');
}
});
이렇게 하면 효과가 있습니다.
$("#btnAddProfile").prop('value', 'Save');
$("#btnAddProfile").button('refresh');
$("#btnAddProfile").html('Save').button('refresh');
맞아요, 저한테는 효과가 있어요.
$('#btnAddProfile').bind('click',function(){
$(this).attr('value','save');
})
Jquery를 사용할 수 있고 코드가 올바른 위치에 있는지 확인하시겠습니까?
$( "#btnAddProfile" ).on( "click",function(event){
$( event.target ).html( "Save" );
});
언급URL : https://stackoverflow.com/questions/5580616/how-to-change-the-text-of-a-button-in-jquery
'programing' 카테고리의 다른 글
| Git의 단계별 변경을 취소하려면 어떻게 해야 합니까? (0) | 2023.05.28 |
|---|---|
| 블록당량이 C#인 경우? (0) | 2023.05.28 |
| 셀을 편집할 때 키를 눌렀을 때 발생하는 이벤트가 있습니까? (0) | 2023.05.28 |
| __init__ 외부에 새 특성 생성 금지 (0) | 2023.05.28 |
| Git: 로컬 지점만 나열하려면 어떻게 해야 합니까? (0) | 2023.05.28 |