如何更改按钮值?

时间:2013-08-14 09:46:25

标签: jquery html ajax boonex-dolphin

<input onclick="getInvitevalue(__id__, this)" type="button" value="Invite" name="InviteTeacher" class="InviteTeacher" id= "InviteTeacher"/>

<input onclick="getCancelvalue(__id__, InviteTeacher)" type="button" value="Cancel" name="Cancel" class="Cancel" id= "Cancel"/>function getInvitevalue(idValue,invite){var field1 = idValue;
$.ajax({  url: "inviteteacher.php",  type: "POST",  data: ({name: field1}),}).done(function() {
if(invite.value = 'Invite')
invite.value = 'Pending Request';});}function getCancelvalue(idValue,cancel){var field2 = idValue;
$.ajax({  url: "inviteteacher.php",  type: "POST",  data: ({name: field2}),}).done(function() { 
if(cancel.value = 'Pending Request')
cancel.value = 'Invite';});}

这里用于显示动态值的onclick id。请查看我的代码并说明如何在boonex-Dolphin中更改它?

2 个答案:

答案 0 :(得分:0)

更短的代码 - &gt;不需要在div中包装butoons

new shorter DEMO

$('.b2').attr('disabled', true);
$('.b1').click(function () {
    this.value = 'Pending Request';
    $(this).next('.b2').removeAttr('disabled');
});
$('.b2').click(function () {
    $(this).attr('disabled', true).prev('.b1').val('Invite');
});

现在使用按钮的按钮和按钮包含在div

Updated Demo

<强> HTML

<div>
    <input type="button" class="b1" value="Invite" />
    <input type="button" class="b2" value="Cancel" />
</div>

$('.b2').attr('disabled', true);
$('.b1').click(function () {
    this.value = 'Pending Request';
    $(this).parents('div').find('.b2').removeAttr('disabled');
});
$('.b2').click(function () {
    $(this).attr('disabled', true).parents('div').find('.b1').val('Invite');
});

$('#b1')是指element,ID为b1

this.value = 'Pending Request'更改了element代码更改值b1下方点击的当前元素的文字

DEMO

<强> HTML

<input type="button" id="b1" value="Invite" />
<input type="button" id="b2" value="Cancel" />

<强> JS

$('#b1').click(function () {
    this.value = 'Pending Request';
});
$('#b2').click(function () {
    $('#b1').val('Invite');
});

答案 1 :(得分:0)

这是 jsFiddle demo ,我还在没有请求运行时将取消按钮设置为禁用等等。

<强> HTML:

<button id="btn_start_request">Invite</button>
<button id="btn_cancel_request">Cancel</button>

<强> JS:

function startRequest() {
    $("#btn_start_request").text("Pending request ..").attr("disabled", true)
    $("#btn_cancel_request").attr("disabled", false)

    // start request here
}
function cancelRequest() {
    $("#btn_start_request").text("Invite").attr("disabled", false)
    $("#btn_cancel_request").bind("click", cancelRequest).attr("disabled", true)

    // cancel request here
}

$(function() {
    $("#btn_start_request").bind("click", startRequest)
    $("#btn_cancel_request").bind("click", cancelRequest).attr("disabled", true)
})