2012-11-17 50 views
0

在下面的代碼中,有這樣的javascript函數sendPushNotification(id)其中有這樣的行var data = $('form#'+id).serialize()$('form#'+id)是什麼意思?沒有形式下面具有ID「#形式」 + ID ...

<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function(){ 

      }); 
      function sendPushNotification(id){ 
       var data = $('form#'+id).serialize(); 
       $('form#'+id).unbind('submit'); 
       $.ajax({ 
        url: "send_message.php", 
        type: 'GET', 
        data: data, 
        beforeSend: function() { 

        }, 
        success: function(data, textStatus, xhr) { 
          $('.txt_message').val(""); 
        }, 
        error: function(xhr, textStatus, errorThrown) { 

        } 
       }); 
       return false; 
      } 
     </script> 

    </head> 
    <body> 
     <?php 
     include_once 'db_functions.php'; 
     $db = new DB_Functions(); 
     $users = $db->getAllUsers(); 
     if ($users != false) 
      $no_of_users = mysql_num_rows($users); 
     else 
      $no_of_users = 0; 
     ?> 
     <div class="container"> 
      <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1> 
      <hr/> 
      <ul class="devices"> 
       <?php 
       if ($no_of_users > 0) { 
        ?> 
        <?php 
        while ($row = mysql_fetch_array($users)) { 
         ?> 
         <li> 
          <form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')"> 
           <label>Name: </label> <span><?php echo $row["name"] ?></span> 
           <div class="clear"></div> 
           <label>Email:</label> <span><?php echo $row["email"] ?></span> 
           <div class="clear"></div> 
           <div class="send_container"> 
            <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea> 
            <input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/> 
            <input type="submit" class="send_btn" value="Send" onclick=""/> 
           </div> 
          </form> 
         </li> 
        <?php } 
       } else { ?> 
        <li> 
         No Users Registered Yet! 
        </li> 
       <?php } ?> 
      </ul> 
     </div> 
    </body> 
</html> 

回答

1

它選擇與變量id的值的一個id元件form。假設它是有效的,因爲沒有重複的ID並且id將永遠不是form的東西id,它也可以簡寫爲$('#' + id)

+1

如果沒有重複的ID,那並不總是意味着'$(「form#id」)'可以寫成'$(「#id」)'。 – pimvdb

+0

@ pimvdb:嗯,是的,但在它總是存在的情況下,它可以。 – icktoofay