在codeginter中我的ajax或jquery中的问题

时间:2018-04-26 07:26:48

标签: php jquery ajax

兄弟我插入图片编辑更新工作正常,但我的问题是,当我点击编辑并删除一些图像,这是ajax调用,但它给出一些错误,我不知道为什么这显示此错误任何兄弟的帮助请向我表示感谢

这是我的编辑代码 -

        <span style="cursor:pointer;" onclick="javascript:deleteimage(<?php echo $data['id'] ?>)">X</span>
        </td>
    </tr>
    <?php }endif; ?>
    <tr>
        <td>Images</td>
        <td><input type="file" name="userfile[]" id="image_file" accept=".png,.jpg,.jpeg,.gif" multiple></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input style="width: 50%;" type="submit" value="Submit"></td>
    </tr>
</table>
</form>
<script>
    $(document).ready(function() {

    function deleteimage(image_id) {

        var answer = confirm("Are you sure you want to delete from this post?");
        if (answer) {
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('welcome/deleteimage');?>",
                data: "image_id=" + image_id,
                success: function(response) {
                    if (response == 1) {
                        $(".imagelocation" + image_id).remove(".imagelocation" + image_id);
                    };

                }
            });
        }
    }

});
</script>

结束显示我的错误是

  

未捕获的ReferenceError:未定义deleteimage       在HTMLSpanElement.onclick(VM1166 5:35)

2 个答案:

答案 0 :(得分:1)

这是正确答案

<script>
function deleteimage(image_id) {

        var answer = confirm("Are you sure you want to delete from this post?");
        if (answer) {
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('welcome/deleteimage');?>",
                data: "image_id=" + image_id,
                success: function(response) {
                    if (response == 1) {
                        $(".imagelocation" + image_id).remove(".imagelocation" + image_id);
                    };

                }
            });
        }
    }
</script>

答案 1 :(得分:0)

尝试改变这个:

onclick="javascript:deleteimage(<?php echo $data['id'] ?>)

到此:

onclick="deleteimage(\"<?php echo $data['id'] ?>\")

将功能移到document.ready

之外