如果表单提交成功,我该如何更改按钮的行为?
<form method="post" enctype="multipart/form-data" style="margin: 0;" id="frmGenerate">
<div class="clearfix">
<div class="input-group">
<input type="text" placeholder="Customer Name:" name="CustomerName">
<input type="text" placeholder="Generated Url:" name="CustomerUrl" readonly="readonly" />
</div>
</div>
<div class="clearfix">
<div class="input-group">
<button type="submit" class="btn">Generate</button>
</div>
</div>
</form>
这是我的JS代码:
$('#frmGenerate').submit(function(e) {
var clientName = $(this).find('input[name="CustomerName"]').val();
$.ajax(
{
url: '@Url.Action("GenerateToken","Admin")',
type: 'GET',
data: { customerName: clientName },
success: function(response) {
if (response.result) {
$('#frmGenerate').find('input[name="CustomerUrl"]').val(response.message).select();
$('#frmGenerate').find('button.btn').text('Close');
} else {
alert(response.message);
}
}
});
e.preventDefault();
});
这是模态窗口,我打开它来生成令牌。当它生成成功然后我在模态输入中设置生成的令牌并将按钮的文本更改为关闭,但我不知道如何更改按钮的行为。
答案 0 :(得分:1)
更好的方法不是更改现有按钮,而是隐藏,显示其他按钮。
HTML:
<div class="clearfix">
<div class="input-group">
<button type="submit" class="btn">Generate</button>
<button type="button" class="btn btn-close hide">Close</button>
</div>
</div>
JS:
$('#frmGenerate').find('.btn').hide();
$('#frmGenerate').find('.btn-close').show();
答案 1 :(得分:0)
取决于您使用的按钮类型。必须使用按钮,如:<button></button>
不像<button/>
然后使用:
$(".btn").html('Close');
答案 2 :(得分:0)
您可以更改按钮的属性
$('#frmGenerate').find('button.btn').attr('type', 'button');
然后:
$('#frmGenerate').find('button.btn').on('click', function(){ /*close function */});
答案 3 :(得分:0)
我发现您在提交表单后已将按钮文本更改为“关闭”。所以剩下的部分是处理按钮上的点击事件的jQuery。
...
$('.btn').click(function(){
var buttonText = $(this).val();
if(buttonText === 'Close'){
//code when close button is clicked
}else if(buttonText === 'Generate'){
//code when Generate button is clicked...
}
});//end button.click function
我会使用input type ='button'或按钮而不是输入type ='submit'。为什么不使用ajax提交表单?好多了。
希望这会有所帮助......