forEach在onClick上运行良好,但不是没有

时间:2017-01-31 21:09:51

标签: javascript jquery

当我尝试将# aim of script is to compare csv files and output difference as a new csv # import necessary libraries import csv # File1 = open(raw_input("path:"),"r") #filename, mode # File2 = open(raw_input("path:"),"r") #filename, mode # selects the 2 input files to be compared input_file1 = "G:/savestuffhereqwerty/electorate_meshblocks/teststuff/Book1.csv" input_file2 = "G:/savestuffhereqwerty/electorate_meshblocks/teststuff/Book2.csv" # creates the blank output csv file output_path = "G:/savestuffhereqwerty/electorate_meshblocks/outputs/output2.csv" a = open(input_file1, "r") output_file = open(output_path,"w") output_file.close() count = 0 with open(input_file1) as fp1: for row_number1, row_value1 in enumerate(fp1): if row_number1 == count: print "got to 1st point" value1 = row_value1 with open(input_file2) as fp2: for row_number2, row_value2 in enumerate(fp2): if row_number2 == count: print "got to 2nd point" value2 = row_value2 if value1 == value2: print value1, value2 else: print value1, value2 with open(output_path, 'wb') as f: writer = csv.writer(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) # testing to see if the code writes text to the csv writer.writerow(["test1"]) writer.writerow(["test2", "test3", "test4"]) writer.writerows([value1, value2]) print "code reached writing stage" count += 1 print count print "done" # replace(",",".") 属性附加到服务器列表时,它不会执行<li> - 我尝试嵌套了forEach()但它忽略了它{ {1}}是一个空数组?

&#13;
&#13;
console.log()
&#13;
Servers
&#13;
$(function() {

  var Discord = {
    Servers: [{
      Name: 'Server 1',
      Link: 'hidden for security'
    }, {
      Name: 'Server 2',
      Link: 'hidden for security'
    }]
  };

  $('#_discord_notify_btn').click(function() {
    Discord.Servers.forEach(function($server) {
      $.post($server.Link, {
        content: $('#_discord_notify').val()
      });
    });
  });
  
  // this code is ignored??
  Discord.Servers.forEach(function($server) {
    $('_discord_servers ul').append("<li>" + $server.Name + "</li>");
  });

});
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:6)

在第二个示例中,您似乎错过了ID选择器的#。

// this code is ignored??
  Discord.Servers.forEach(function($server) {
    $('#_discord_servers ul').append("<li>" + $server.Name + "</li>");
  });

答案 1 :(得分:0)

您错过了ID选择器:

&#13;
&#13;
$(function() {

  var Discord = {
    Servers: [{
      Name: 'Server 1',
      Link: 'hidden for security'
    }, {
      Name: 'Server 2',
      Link: 'hidden for security'
    }]
  };

  $('#_discord_notify_btn').click(function() {
    Discord.Servers.forEach(function($server) {
      $.post($server.Link, {
        content: $('#_discord_notify').val()
      });
    });
  });
  
  // this code is ignored??
  Discord.Servers.forEach(function($server) {
    console.log('test');
    $('#_discord_servers ul').append("<li>" + $server.Name + "</li>");
  });

});
&#13;
textarea {
  min-width: 447px;
  min-height: 110px;
}

button {
  border: 1px solid #000;
  border-radius: 9px;
  background: none;
  padding: 10px;
}

button:hover {
  background: #000;
  color: #fff;
  border: 1px solid #fff;
  cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='_discord_servers'>
  <b>Servers you're notifying</b>
  <ul></ul>
</div>
<textarea id='_discord_notify'>Enter your notification...</textarea>
<button type='button' id='_discord_notify_btn'>
  Send Notification
</button>
&#13;
&#13;
&#13;