javascript
  • jquery
  • 2015-09-14 146 views 0 likes 
    0

    我遇到類似的問題this。點擊後第一行數增加,但點擊後第二行數不增加。點擊後我希望所有的數字都增加。我認爲jquery代碼是錯誤的,請有人幫我修復我的jQuery代碼。號碼不增加

    我的代碼:

    <?php 
        include("mwcon.php"); 
    ?> 
    <html> 
    <head> 
        <link rel='stylesheet' type='text/css' href='/css/post.css'> 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
        <script> 
         $(document).ready(function(){ 
          $("#post-vote-top").click(function() { 
           $("#post-vote-middle").text(function(){ 
            return ~~$(this).text() + 1 
           }); 
          });    
         }); 
        </script> 
    </head> 
    <body> 
    <?php 
        echo " 
         <div id='post'> 
          <table> 
        "; 
        $no=1; 
        $q=mysql_query("SELECT * FROM t_post"); 
        while($f=mysql_fetch_object($q)){ 
         echo " 
          <tr> 
           <td id='post-nomor'>$no.</td> 
           <td id='post-vote' align='center'> 
            <div id='main'> 
             <div id='post-vote-top'></div> 
             <div id='post-vote-middle'>$f->vote</div> 
             <div id='post-vote-bottom'></div> 
            </div> 
           </td> 
           <td><a href='$url' target='_blank'><img src='$f->linkimg' width='80' height='50'></img></a></td> 
          </tr> 
         "; 
         $no++; 
        } 
        echo " 
          </table> 
         </div> 
        "; 
    ?> 
    </body> 
    </html> 
    

    請幫助我。謝謝。

    回答

    2

    id應該是唯一使用class代替。您可以使用next()選擇元素一見如故之後元素

    $(document).ready(function() { 
     
        $(".post-vote-top").click(function() { 
     
        $(this).next(".post-vote-middle").text(function(i, v) { 
     
         return parseInt(v, 10)+1; 
     
        }); 
     
        }); 
     
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
     
    <table> 
     
        <tr> 
     
        <td id='post-nomor'>$no.</td> 
     
        <td id='post-vote' align='center'> 
     
         <div class='main'> 
     
         <div class='post-vote-top'>up</div> 
     
         <div class='post-vote-middle'>1</div> 
     
         <div class='post-vote-bottom'></div> 
     
         </div> 
     
        </td> 
     
        <td></td> 
     
        </tr> 
     
        <tr> 
     
        <td id='post-nomor'>$no.</td> 
     
        <td id='post-vote' align='center'> 
     
         <div class='main'> 
     
         <div class='post-vote-top'>up</div> 
     
         <div class='post-vote-middle'>1</div> 
     
         <div class='post-vote-bottom'></div> 
     
         </div> 
     
        </td> 
     
        <td></td> 
     
        </tr> 
     
    </table>

    +0

    等待,我會盡量 –

    +0

    大。它現在可以工作,你是如此聰明@pranav。非常感謝。 –

    +0

    @serbaserbi樂於提供幫助 –

    相關問題