代码之家  ›  专栏  ›  技术社区  ›  Siddharth

onClick不调用按钮(html)

  •  1
  • Siddharth  · 技术社区  · 7 年前

    <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>
    

    <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( onClick="unPublish", function unPublish() 我也是。仍然是 href of <a> 接到电话。有办法覆盖主div <a href> 而是在点击时呼叫按钮的调用者?

    我已经看完了 this

    现在我知道我错过了一些很基本的东西。

    编辑1

    <div onclick="location.href='onLoadShowRoutesView?routeIdnpk=${param.routeIdnpk}';">
    

    即使单击按钮,onLoadShowRoutesView仍会被调用

    编辑2 @扎卡莉亚·阿恰尔基:我还补充说

    function onUnPublish () {
        var routeIdnpk = $(this).prev().val();
    
                $.ajax({
            }) ;
    }
    
    function publish () {
        var routeIdnpk = $(this).prev().val();
        $.ajax({
            }) ;
    }
    

    同样的结果

    2 回复  |  直到 7 年前
        1
  •  1
  •   Siddharth    7 年前

    在@zakaria acharki的帮助下,下面是对我有用的。一些拼写错误和“ondblclick”与父级 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>
    

    <div
        ondblclick="location.href='onLoadShowRoutesView?routeIdnpk=${param.routeIdnpk}';">
    

    编辑

    只适用于第一个按钮

    $('button[name=“unPublish”]')。单击(function(){ 这对所有的按钮都有效

    今天:我学到了 button[name="something"]

        2
  •  0
  •   Michael Chandra Lrc    7 年前

    试着像这样编辑你的代码

    <a href="javascript:void(0);" onclick="yourFunction()"></a>
    

    可以在元素上添加onclick函数,也可以动态添加该函数