简体   繁体   中英

JQuery replacewith not working after Ajax call

Im doing a AJAX call and once I get the response, I want to replace a submit button with a label...But this is not working. I get the alert msg ie; the response from the Ajax call but the replacewith command fails.

Can you please tell me what is the mistake Im doing..

Replacewith command

$( this ).closest( 'tr' ).find( 'input:label' ).replaceWith("<label for=\'success\'>SUCCESS</label>");

Code:

$("#table_appl,#table_enfr,#table_det01,#table_det02,#table_det03,#table_det04,#table_det05,#table_datais").on( "click", "input:submit", function( event ) {
    //alert('Hi')
    //event.preventDefault();
    var fieldvalue = $(this).closest('tr').find('input:text').val();
    var fieldname = $(this).closest('tr').find('input:text').attr('name');
    alert('fieldname = ' + fieldname)
    alert('fieldvalue = ' + fieldvalue)
      $.ajax({

            type:"GET",

            url:"/validate/",
            data:{'fieldvalue':fieldvalue,'fieldname':fieldname},
            success:function(data){
                if (data == 'Y'){
                    var item  = $(this).closest('tr').find('input:label')
                    alert("Gonna be replaced" + item);
                    $( this ).closest( 'tr' ).find( 'input:label' );
                    $( this ).closest( 'tr' ).find( 'input:label' ).replaceWith("<label for=\'success\'>SUCCESS</label>");


                }
                alert("Response = "+ data);
            }

       });

    return false;


})

Here's a fiddle .

And here's the code:

<form role="form" method="POST" action="/validate/" id="input_form">
    <input id="myInput">
    <button type="submit" id="submitButton">Click me!</button>
</form>

with the script:

$("#submitButton").click(function () {
    event.preventDefault();
    $.get("/validate/", function (d){    
        if (d == "Y"){        
            $("#submitButton").replaceWith("<label>SUCCESS</label>");
        }                    
    })
})

Is this about what you want? I guess you'll have to adapt the .replaceWith() part to find the correct element...

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