我的 html fragmentRow 如下所示,它有一个 <a>
和一个
<a href="onLoadShowRoutesView?routeIdnpk=${param.routeIdnpk}">
<div class="col-md-4 pull-right">
<input type="button" class="btn btn-small btn-default btn-flat pull-right" value="${param.publishOrUnPublishValue}"
name="${param.publishOrUnPublishName}"
id="${param.publishOrUnPublishName}"
onclick="${param.publishOrUnPublishOnClick}"
data-val="${param.routeIdnpk}" style="margin-top: 6px;">
</div>
</a>
我像这样包含 fragmentRow.jsp
<jsp:include page="fragmentListRouteItem.jsp">
<jsp:param name="routeIdnpk"
value="${childRouteViewPojo.routeIdnpk}" />
<jsp:param name="publishOrUnPublishValue" value="Publish" />
<jsp:param name="publishOrUnPublishName" value="Publish" />
<jsp:param name="publishOrUnPublishOnClick" value="onPublish" />
</jsp:include>
我有一个 ajax
那叫onclick
按钮的
$('#unPublish').click(
function() {
var routeIdnpk = $(this).prev().val();
$.ajax({
type : "Get",
url : "unpublishRoute",
data : {
routeIdnpk : routeIdnpk
},
success : function(response) {
response = successAction(response);
},
error : function(e) {
ajaxGlobalErrorResponseWithTitleAndMessage("Unable to Unpublish",
"The unpublish operation failed, please try again");
}
});
});
当我点击按钮时,onLoadShowRoutesView
被调用而不是 $('#unPublish').click(
我试过了$('#unPublish').click(
和 onClick="unPublish", function unPublish()
也。还是href of <a>
被调用。有没有办法覆盖主 div <a href>
而是在点击时调用按钮的调用者?
我已通读 this ,以及其他链接。但无法找到问题的解决方案。
现在我知道我在这里遗漏了一些非常基本的东西。
编辑 1:我试过了
<div onclick="location.href='onLoadShowRoutesView?routeIdnpk=${param.routeIdnpk}';">
即使我点击按钮,onLoadShowRoutesView 仍然被调用
编辑 2 @zakaria-acharki:我还添加了
function onUnPublish () {
var routeIdnpk = $(this).prev().val();
$.ajax({
}) ;
}
function publish () {
var routeIdnpk = $(this).prev().val();
$.ajax({
}) ;
}
同样的结果
最佳答案
在@zakaria-acharki 的帮助下,以下内容对我有用。父 div
1
$('#unPublish').click(
function() {
var routeIdnpk = $(this).val();
$.ajax({
});
$('#Publish').click(
function() {
var routeIdnpk = $(this).val();
$.ajax({
});
2
<button type="button"
class="btn btn-small btn-default btn-flat pull-right"
value="${param.routeIdnpk}"
name="${param.publishOrUnPublishName}"
id="${param.publishOrUnPublishName}" style="margin-top: 6px;">${param.publishOrUnPublishValue}</button>
3
<div
ondblclick="location.href='onLoadShowRoutesView?routeIdnpk=${param.routeIdnpk}';">
编辑
$('#unPublish').click( works only for the first button
$('button[name="unPublish"]').click(function(){ this worked for all the buttons
今天:我了解了 button[name="something"]
的值(value)
关于javascript - 按钮不调用 onClick (html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51379616/