javascript
  • jquery
  • 2015-09-14 30 views -1 likes 
    -1

    我在這裏有我的問題video。向上箭頭點擊後數字增加,但反之亦然。我認爲jQuery代碼是不正確的。我以爲我只是從頂部底部,並從+1到-1,但我錯了,它不能很好地工作。點擊向下箭頭後數字不會減少

    代碼:

    <?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() { 
          $(this).next(".post-vote-middle").text(function(i, v) { 
           return parseInt(v, 10)+1; 
          }); 
          }); 
         }); 
         $(document).ready(function() { 
          $(".post-vote-bottom").click(function() { 
          $(this).next(".post-vote-middle").text(function(i, v) { 
           return parseInt(v, 10)-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 class='post-nomor'>$no.</td> 
           <td class='post-vote' align='center'> 
            <div id='main'> 
             <div class='post-vote-top'></div> 
             <div class='post-vote-middle'>$f->vote</div> 
             <div class='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> 
    

    請幫助我。謝謝。

    回答

    1

    您需要使用prev(),而不是next(),因爲如果你想避免負值,則使用.post-vote-middle是前.post-vote-bottom

    $(document).ready(function() { 
     
        $(".post-vote-top").click(function() { 
     
        $(this).next(".post-vote-middle").text(function(i, v) { 
     
         return parseInt(v, 10) + 1; 
     
        }); 
     
        }); 
     
        $(".post-vote-bottom").click(function() { 
     
        $(this).prev(".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'>down</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'>down</div> 
     
         </div> 
     
        </td> 
     
        <td></td> 
     
        </tr> 
     
    </table>

    $(document).ready(function() { 
     
        $(".post-vote-top").click(function() { 
     
        $(this).next(".post-vote-middle").text(function(i, v) { 
     
         return parseInt(v, 10) + 1; 
     
        }); 
     
        }); 
     
        $(".post-vote-bottom").click(function() { 
     
        $(this).prev(".post-vote-middle").text(function(i, v) { 
     
         v = parseInt(v, 10); 
     
         if (v > 0) 
     
         return v - 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'>down</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'>down</div> 
     
         </div> 
     
        </td> 
     
        <td></td> 
     
        </tr> 
     
    </table>

    +0

    我很慚愧你@balan。我很抱歉,我是jquery的新手。我根本沒有任何老師。它現在運作良好。也許在接下來的幾個小時裏,我會再問你一次。 –

    +0

    @Junaserbaserbi:很高興幫助:) –

    +0

    Pranav,請再次幫我在[這裏](http://stackoverflow.com/questions/33669459/confusing-about-this-cookies-in-redirecting-system) –

    相關問題