Pregunta

I'm learning $.post in jquery, and after many hours I was thrilled that it worked.

The problem is that it only works on the first execution of the page, or if I manually refresh the page.

Is it possible that location.reload(); isn't working properly or perhaps location.reload() isn't the right thing here?

Any help appreciated. I'm dead stuck.

Here's my code:

<script type="application/x-javascript">

    $("a#edit").click(function(e){
        e.preventDefault();
        var id;
        id = $(this).attr('sid');
        $.modal.prompt('Editing Source ID: ' + id,
            function(){
                $.post("<?php echo $_SERVER['PHP_SELF']; ?>", { 
                                        up: $('#prompt-value').attr('value'),
                                        sid: id                                         
                                        })
                notify('Notification', 'Source ' + id + ' changed to: ' + $('#prompt-value').attr('value'), {
                    system: true,
                    icon: 'img/demo/icon.png',
                    iconOutside: false
                });
                location.reload();
             });
    });

</script>

I'm at a total loss.

¿Fue útil?

Solución

Try adding a console.log($("a#edit")) before you attach the click event.

This code is confusing since you appear to be AJAX posting to the current file, then refreshing, kind of makes the ajax post useless, might as well have posted to itself.

Most jquery functions are async, use the success callback or deferred done if the notification should only be executed on success.

$.post("<?php echo $_SERVER['PHP_SELF']; ?>", 
    { 
        up: $('#prompt-value').attr('value'),
        sid: id                                         
    }        
).done(function (data) {
    // Success, read response data, send notification if applicable and use 
    // the response data to change the current page without having to refresh
}).fail(function () {
    // Failure on the request, handle it
});

Otros consejos

Use

 $("a#edit").live('click',function(e){
 ...
 });

jQuery 1.4.+ i think

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top