jQuery代码没有产生预期的结果

时间:2015-11-05 04:56:24

标签: jquery

我尝试编写代码HTML,看起来像

<?php

// array for JSON response
$response = array();

$to = $_POST['rcpt'];
$subject = $_POST['subject'];
$body = $_POST['msgBody'];
$cc = $_POST['cc'];

send_email($to,$subject,$body,$cc);

function send_email($to,$subject,$body,$cc = ''){
  require_once "Mail.php";

  $from = "monitor<monitor@m2techtronix.com>";

  $host = "smtp.domain.com";
  $username = "monitor@m2techtronix.com";
  $password = "gUZfE6SVLV";

//  $headers = array ('From' => $from,
//  'To' => $to,
//  'Subject' => $subject,
//  'Cc' => $cc);
  $headers['From']    = $from;
  $headers['To']      = $to.", ".$cc;
  $headers['Subject'] = $subject;


  $smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

  $mail = $smtp->send($to, $headers, $body);

  if (PEAR::isError($mail)) {
    $response["success"] = 0;
    $response["message"] = $mail->getMessage();
    echo json_encode($response);

  }else {
    $response["success"] = 1;
    $response["message"] = "Email sent to " . $to;
    echo json_encode($response);
  }
}
?>

我有两个元素HTML

 socket.on('listUser', function(data){
          console.log(data);
          console.log(socket.id);
          for(var i =0 ; i<data.length; i++){
            $('#listUser').append('<a href ="#" id="'+data[i].name+'" class="list-group-item" value="'+data[i].socketId+'">' +data[i].name+ '</a>'); 
          }
        });

        $('a').each(function(){
          console.log($(this).attr('id'));
          /*$(this).click(function(){

          });*/
        });

但是当我尝试运行我的代码js时,我只得到2个值:id = list和id = message。那是为什么?

1 个答案:

答案 0 :(得分:1)

这是我的代码:

<!DOCTYPE html>
<!-- saved from url=(0040)http://getbootstrap.com/examples/signin/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="http://getbootstrap.com/favicon.ico">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" >

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" >
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <title>Signin</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <style type="text/css">
    #viewContent{
      color: black ; 
      height: 400px;
      background-color: #F5F5F5;
      overflow: scroll;
    }
    #listUser{
      height: 400px;

    }
    </style>
  </head>

  <body>

    <div id='header'>
      <h2 class="title">This is chat box</h2>
    </div>

    <div id='content' >
      <div id='listUser' class='col-md-4'>
        <a  id ='list' class = "list-group-item active">List User</a>
      </div>

      <div name='chatbox' class='col-md-8'>
        <a  id ='message' class = "list-group-item active">List User</a>
        <div id='viewContent'>
        </div>

        <div name='formInput'  >
          <form class='' id='formChat'>
            <div class="form-group">
              <label class="sr-only" for="contentChat">Enter message</label>
              <input type="text" class="form-control" id="contentChat" placeholder="Enter message" >
              <input type='submit' class='btn btn-primary ' value ='Send'>
            </div>
          </form>
        </div>

      </div> <!-- chat box div -->
    </div>

    <script type="text/javascript">
      jQuery(function($){        
        var socket = io.connect('http://localhost:8080');
        var $contentChat = $('#contentChat'),
            $send =$('formChat');
        var emailLogin = '';
        var listID = [];
        var idSocket ;

        socket.on('listUser', function(data){
          console.log(data);
          console.log(socket.id);
          for(var i =0 ; i<data.length; i++){
            $('#listUser').append('<a href ="#" id="'+data[i].name+'" class="list-group-item" value="'+
              data[i].socketId+'">' +data[i].name+ '</a>').click(function(){
                showSocketid(data[i].socketId,data[i].name);
            }); 
          }
        });

        $('a').each(function(){
          console.log($(this).attr('id'));
          /*$(this).click(function(){
          });*/
        });

        function showSocketid(socketID , nameUser){
          alert(socketID +'and'+ nameUser);
        };

      });
    </script>

  </body>
</html>