dongpo7467 2014-05-20 14:54
浏览 73
已采纳

无法获取按钮的ID

Please have a look at the following code

<?php
//echo $this->Html->css(array('bootstrap', 'mark', 'style'));
echo $this->Html->script(array('timer','swfobject','bootstrap.min.js'));



?>
<style>
#hideall {
    display: none;
    opacity: 0.7;
    position: fixed;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    background: #000;
    border: 1px solid #cecece;
    z-index: 1;
    vertical-align:middle;
    text-align:center;
}

.removeCardflip{
    transition: rotateY(0deg);
    -webkit-transition: rotateY(0deg);
    transition-duration: 0s;    
}




/*  SECTIONS  */
.section {
    clear: both;
    padding: 0 10px 0 10px;
    margin: 0px;
}


</style>


<div id="hideall">
    <?php //echo $this->Html->image('progress.gif', array('alt' => 'Wait', 'style' => 'text-align:center; padding-top:200px;'));?>
</div>

<!--<div class="wrapper" style="border: 1px solid red; width: 100%;">-->
    <div class="section group" style="margin-top: 50px;">
        <div class="col span_3_of_3">
            <h3 style="margin:0px; font-size:22px;">Play word game: </h3>
        </div>
    </div>


    <div class="">
        <div>
            <div>
                <span class="remainWords"><?php //echo count($words);?></span> oxxxxxxxxxxxxxxxf <?php //echo $totalWords;?>
            </div>

            <div>
                <?php
                echo $this->Html->image("comic_edit.png",
                                        array(
                                            "alt" => "Pareto List",
                                            "id" => "paretoList",
                                            'url' => "javascript:;"
                                        )
                                    );
                ?>
            </div>
        </div>
    </div>




    <div class="container"><div class="row">






<?php

$word="";

    foreach($worddeck as $worcard)
    {
    ?>
        <div class="xy col-lg-3 col-md-4 col-sm-6 img-rounded" id="card1" style="width:250px; height:200px; background-color:grey; heiht:170px; margin: 10px 10px;">
            <div id="enside1" >
                <h1 data-pos="<?php //echo ; ?>" ><?php echo $worcard['unique_wordsforcards']['en_word']; $enSpell = $worcard['unique_wordsforcards']['en_word']; ?></h1>       
            </div>

            <div id="ptside1" style="display:none;">

            <?php echo $phonemer[$enSpell]; ?>
                <p><?php echo $worcard['unique_wordsforcards']['hint']; ?></p>
            </div>
            <div id="cntrol1">
                <button type="button" id="<?php $enSpell ?>" class="a btn btn-success mr5 btn-lg">Acertei</button>
                <button type="button" id="2" class="e btn btn-danger mr5 btn-lg">Errei</button>
            </div>
        </div>
    <?php
    }
?>





    </div></div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript">
$(document).ready(function(){


   $( ".btn-danger" ).click(function(){
        console.log("Red Button");
        var toclose = $(this).parent().parent();
        $.ajax({
          url: "../img/media.jpg",
        }).done(function() {
            console.log( "The act has been done");
            toclose.toggle();
          });
   }); 


   $( ".btn-success" ).click(function(){
        console.log("Red Button");
        var toclose = $(this).parent().parent();
        $.ajax({
          url: "../img/media.jpg",
        }).done(function() {
                    xxx($(this).attr('id'));
            console.log( "The act has been done");
            toclose.toggle();
          });
   }); 

  $( ".xy" ).click(function(){

    $(this).find("#enside1").toggle();
    $(this).find("#ptside1").toggle();
    console.log(this);
  });

      function xxx(id)
  {
      alert(id);
  }

  function increment()
  {
      $.ajax({
          url: "http://localhost/cake2/flip2/correct",
        }).done(function() {
            console.log( "The act has been done");
            toclose.toggle();
          });

    }

});


</script>

Please consider how those buttons are being generated inside the PHP loop.

I am trying to print the ID of a button using the below code.

 $( ".btn-success" ).click(function(){
        console.log("Red Button");
        var toclose = $(this).parent().parent();
        $.ajax({
          url: "../img/media.jpg",
        }).done(function() {
                    xxx($(this).attr('id'));
            console.log( "The act has been done");
            toclose.toggle();
          });
   }); 

But the alert dialog is empty. What has gone wrong here?

  • 写回答

2条回答 默认 最新

  • dongze8698 2014-05-20 14:55
    关注

    You are using $(this) in the wrong place. Save id of button before ajax call and use in ajax call done() function.

    $( ".btn-success" ).click(function(){
          console.log("Red Button");
          idOfButton = $(this).attr('id');
          var toclose = $(this).parent().parent();
          $.ajax({
            url: "../img/media.jpg",
          }).done(function() {
              alert(idOfButton);
              console.log( "The act has been done");
              toclose.toggle();
          });
     }); 
    

    You can also save the event source before ajax call and later use it to get the id of event source element.

    $( ".btn-success" ).click(function(){
          console.log("Red Button");
          eventSourceButton = $(this);
          var toclose = $(this).parent().parent();
          $.ajax({
            url: "../img/media.jpg",
          }).done(function() {
              alert(eventSourceButton.attr('id'));
              console.log( "The act has been done");
              toclose.toggle();
          });
     }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?