这是我的编辑代码 -
<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)
答案 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