简体   繁体   中英

Change submit button to close button

How can i change behaviour of button if form submit was successful?

<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>

And here is my JS code:

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

This is modal windows, which i open to generate token. When it's generated successful then i set generated token in modal input and change text of button to close, but i don't know how to change button's behaviour.

Better approach would be not to change existing button, but hide it and show another.

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

Depends on what type of button you are using. Button must be used like: <button></button> Not like <button/>

And then use:

 $(".btn").html('Close');

you can change attribute of button

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

and then:

$('#frmGenerate').find('button.btn').on('click', function(){ /*close function */});

I see you have changed the text of your button to 'Close' after the form has been submitted. So the remaining part is the jQuery that handles a click event on the button.

....

     $('.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

And I would use input type='button' or button than input type='submit'. Why dont you submit form using ajax as well? much better.

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM