简体   繁体   中英

Error returned when trying to delete field with ajax in dialog box jquery

I have list of surveys, each of them have edit button, when clicked, the button dialog box pops up with their answers from database, those answers have delete button with them, but I can't make them to work. This is my code. JQUERY:

 $( "#dialog1" ).dialog({ 
    autoOpen: false,
    width:'auto',

   open: function(event, ui){
       $('<button>', {
            'class': 'button'
        })
        .appendTo($(".wrapper"))
        .click(function(){

            var prom=$(this).closest('div').attr('id') ;
            alert(prom);
          $.ajax({
                url: 'admin/deleteField',
                type: 'post',
                data: { idanswer: prom} ,
                success: function () {
                alert("success");

                },
                error: function () {
                    alert("error");

                }
            });
        });
});

PHP

    public function deleteField()
{

    $IdAnswer=(int)$_POST['idanswer']; 

    Answer::DeleteAnswer($IdAnswer);

}

HTML

    <?php
    $answers=$survey->AllAnswers;
    foreach($answers as $answ)
    {
?>
<div class="wrapper" id="<?php echo $answ['Id']; ?>" style="width:auto;">
<input id='answers' name='answers[]' type='text' value='<?php echo $answ['Answer']; ?>'>    
<?php } ?>

When I clicked on delete button of some answer, alert("error") shows up for a second and then dialog box closes and nothing happens.

ok, so I just added return false; at the end of the ajax call and it's working!

 $( "#dialog1" ).dialog({ 
autoOpen: false,
width:'auto',

open: function(event, ui){
   $('<button>', {
        'class': 'button'
    })
    .appendTo($(".wrapper"))
    .click(function(){

        var prom=$(this).closest('div').attr('id') ;
        alert(prom);
      $.ajax({
            url: 'admin/deleteField',
            type: 'post',
            data: { idanswer: prom} ,
            success: function () {
            alert("success");

            },
            error: function () {
                alert("error");

            }
        });
return false;
    });
});

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