Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

javascript - Confirmation Message not working Using Ajax Codeigniter

I am fetching data from database using ajax in codeigniter and now i am trying to put "delete confirmation box" in controller but that button is not working,

Here is controller file/code

$user['result']=$this->crud->AddMember($data);
foreach($user['result'] as $row) {
    echo "<td>";
    <input type="hidden" name="id" value="'.$id.'"> 
    <input type="submit" class="btn btn-danger btn-sm" value="Delete" name="delete" onclick="return confirm('Are you sure you want to delete this item')">
    </form>';
    echo "</td>";
}

Here is view file

<form method="post" name="myForm" class="form-horizontal" id="user_form" enctype="multipart/form-data">
    <input type="text" class="form-control" id="FlatNumber" name="FlatNumber" placeholder="">
    <button type="submit" class="btn btn-primary btn-lg" id="butsave">Add Details</button>
</form>

<script>
$(document).ready(function() {
    $('#butsave').on('click', function() {

        event.preventDefault();  
        var FlatNumber = $('#FlatNumber').val();
        if(FlatNumber == '') {  
            alert("Please enter Flat Number");
        }else{
            $.ajax({
                    url:"<?php echo base_url() . 'index.php/Member/AddRecord'?>",
                    type: "POST",
                    data: {FlatNumber : FlatNumber},
                    dataType: "html",
                    success: function(msg){
                        alert(msg);
                        if (msg == 'exist') {
                            $("#successs").hide();
                        }else{
                            $("#errorr").hide();
                        }
                    }
            });
        }  
    }); 
});
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is because onclick works only for elements who already on this page. after ajax response it will not work so you need to use code like this.

$(document).on("click",".btn-danger", function(){
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...