我正在尝试使用javascript函数按下删除按钮,但该功能似乎无法运行。
这是按钮
<button class="tiny" id="bookmark_delete" onlclick="ajax_bookmark()">Delete</button>
这是JS
function ajax_bookmark(){
var abc = $('#b_delete').val();
var cde = $('#currect_req_b_id').val();
$.ajax({
type: "GET",
url: "delete_bookmark",
data: {
d:abc,
d1:cde
},
error:function(){
},
dataType: 'json',
//beforeSend:function(){},
//complete:function(o,s){},
success:function(data){
location.href ="/bookmarks/bookmark"
},
type: 'get'
});
}
</script>
答案 0 :(得分:1)
您的按钮标记的onclick
属性中似乎有拼写错误:
<button class="tiny" id="bookmark_delete" onlclick="ajax_bookmark()">Delete</button>
应该是:
<button class="tiny" id="bookmark_delete" onclick="ajax_bookmark()">Delete</button>
工作片段:
function foo(){
$('button').html('I was clicked!');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="foo()">DoFoo</button>
答案 1 :(得分:0)
你的HTML中有错误。您正在使用onlclick而不是onclick:
<button class="tiny" id="bookmark_delete" onclick="ajax_bookmark()">Delete</button>
答案 2 :(得分:0)
您的代码中有错误。使用onclick()
也是一种不好的做法,所以你应该这样做:
HTML:
<button class="tiny" id="bookmark_delete">Delete</button>
JS:
$( document ).ready(function() {
$('#bookmark_delete').on('click', function(){
var abc = $('#b_delete').val();
var cde = $('#currect_req_b_id').val();
$.ajax({
type: "GET",
url: "delete_bookmark",
dataType: 'json',
data: {
d: 'abc',
d1: 'cde'
},
success:function(data){
location.href ="/bookmarks/bookmark"
},
error: function (request, status, error) {
alert(request.responseText);
}
});
});
希望它有所帮助。