循环到变量然后打印var

时间:2016-09-14 13:04:03

标签: javascript

我尝试根据#include "EtherServer.h" #define PORT "31107" // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold int EtherServer::s_sockfd = 0; void EtherServer::sigchld_handler(int s) { // waitpid() might overwrite errno, so we save and restore it: int saved_errno = errno; while(waitpid(-1, NULL, WNOHANG) > 0); errno = saved_errno; } // get sockaddr, IPv4 or IPv6: void* EtherServer::get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } bool EtherServer::init() { int rv; int yes=1; struct addrinfo hints, *servinfo, *p; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((EtherServer::s_sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("server: socket"); continue; } if (setsockopt(EtherServer::s_sockfd, SOL_SOCKET, SO_REUSEADDR, yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(EtherServer::s_sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(EtherServer::s_sockfd); perror("server: bind"); continue; } break; } freeaddrinfo(servinfo); // all done with this structure if (p == NULL) { fprintf(stderr, "server: failed to bind\n"); exit(1); } if (listen(EtherServer::s_sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } struct sigaction sa; sa.sa_handler = EtherServer::sigchld_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } pthread_attr_t attr_baseb; (void)pthread_attr_init(&attr_baseb); (void)pthread_attr_setdetachstate(&attr_baseb, PTHREAD_CREATE_DETACHED); (void)pthread_create(NULL, &attr_baseb, &runServer, (void *)this); } void* EtherServer::runServer(void *arg) { int new_fd; // listen on sock_fd, new connection on new_fd //struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; char s[INET6_ADDRSTRLEN]; printf("server: waiting for connections...\n"); while(1) { // main accept() loop sin_size = sizeof their_addr; sleep(1); new_fd = accept(EtherServer::s_sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, EtherServer::get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s\n", s); if (!fork()) { // this is the child process int n; char buffer[256]; bzero(buffer, 256); close(EtherServer::s_sockfd); // child doesn't need the listener while (n = read(new_fd, buffer, 255) > 0) { if (send(new_fd, buffer, 255, 0) == -1) perror("send"); } close(new_fd); exit(0); sleep(1); } close(new_fd); // parent doesn't need this } } & id具有特定name的字段。但由于多年的jQuery,我的JS有点生锈,我遇到了一些麻烦。

Bellow我的代码和here是js的小提琴。

首先,我要检查class,然后如果类click存在,则运行循环以找到invalid&具有类name attribute的每个元素的ID attribute

然后将其添加到invalid并打印array

足够简单,但似乎并没有打球。我责备多年使用jQuery,因为我应该能够在JS中弹出像循环一样简单的东西没问题。但是,我感到茫然,似乎无法找到错误,因为我没有将打印输出到控制台日志。



array

document.getElementById("submiter").onclick = function() {
  errorLoop();
};

function errorLoop() {
    var errClass = element.classList.contains("invalid"),
      runner = document.document.getElementsByClassName("invalid"),
      dataL = [];


    if (errClass) {

      for (var i = 0; i < runner.length; i++) {
        var errName = runner.getAttribute("name"),
          errId = runner.getAttribute("id");

        dataL.push("Name: " + errName + " - ID: " + errId +", ");
      } //End for

      dataL.toString();

      console.debug(dataL);

      dataLayer.push({
        "validationError": "datal"
      });

    } //End if
  } //End errorLoop
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

根据您的要求更改了代码

function foo() {
		errorLoop();
	}

	function errorLoop() {
		var runner = document.getElementsByClassName("invalid"), dataL = [];
		

		if (runner) {

			for (var i = 0; i < runner.length; i++) {
				var errName = runner[i].getAttribute("name"), errId = runner[i]
						.getAttribute("id");

				dataL.push("Name: " + errName + " - ID: " + errId);
			} //End for

			dataL.toString();
          console.log(dataL)

			console.debug(dataL);

			

		} //End if
	} //End errorLoop
<input type="radio" id="tiMs" name="ti" value="Ms" tabindex="16"
		class="invalid">
	<input type="radio" id="tiMiss" name="ti" value="Miss" tabindex="17"
		class="invalid">
	<input type="radio" id="tiMiss" name="hol" value="Miss" tabindex="17"
		class="invalid">
	<a href="#" id="submiter" onclick="foo()"> <strong>clickit</strong>
	</a>

答案 1 :(得分:0)

您可以尝试以下更改:

function errorLoop() {
   var errClass = element.classList.contains("invalid"),
   runner = document.getElementsByClassName("invalid"), // removed document
   dataL = [];


if (errClass) {

  for (var i = 0; i < runner.length; i++) {
    var errName = runner[i].getAttribute("name"), //used index
      errId = runner[i].getAttribute("id");

    //....
  } //End for

}

  //....
}

答案 2 :(得分:0)

试试这可能会对你有所帮助

 $(document).ready(function(){
 var arrNumber = new Array();
 $('#submiter').on('click',function(){
  $('input[type=radio]:checked').each(function(){
   if($(this).attr('class')=='invalid'){
      arrNumber.push($(this).attr('id')+' => '+$(this).attr('name'));
     }
   });
   console.log(arrNumber);
  });

});