1

如果表单提交成功,如何更改按钮的行为?

<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();
    });

这是模态窗口,我打开它来生成令牌。当它生成成功时,我在模态输入中设置生成的令牌并将按钮的文本更改为关闭,但我不知道如何更改按钮的行为。

4

4 回答 4

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();
于 2014-07-22T11:11:44.293 回答
0

取决于您使用的按钮类型。按钮必须使用如下:<button></button>不喜欢<button/>

然后使用:

 $(".btn").html('Close');
于 2014-07-22T11:11:22.187 回答
0

您可以更改按钮的属性

 $('#frmGenerate').find('button.btn').attr('type', 'button');

进而:

$('#frmGenerate').find('button.btn').on('click', function(){ /*close function */});
于 2014-07-22T11:20:41.133 回答
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' 或 button 而不是 input type='submit'。为什么不也使用 ajax 提交表单?好多了。

希望这可以帮助...

于 2014-07-22T11:22:04.940 回答