在in循环中添加一个事件监听器

时间:2018-01-14 22:31:28

标签: javascript for-loop callback

@KafkaListener(
  topicPartitions = @TopicPartition(topic = "topicName",
  partitionOffsets = {
    @PartitionOffset(partition = "0", initialOffset = "0"), 
    @PartitionOffset(partition = "3", initialOffset = "0")
}))
public void listenToParition(
  @Payload String message, 
  @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
      System.out.println(
        "Received Messasge: " + message"
        + "from partition: " + partition);
}

大家好,我有三个输入function listeners(){ validation = new Validator(); for(input in inputs){ el = $("#"+input+""); addEvent(el, "input", function(){ inputs[input].value = el.value; if(validation.fails(inputs[input])){ //Do something } }) } } ,我想附加一个事件监听器并对每个输入进行一些验证。上面的代码有效,但它总是返回最后一个输入(注释)。请问有什么解决方案可以解决这个问题吗?我想过回调,但我不知道如何在这种情况下实现它。

1 个答案:

答案 0 :(得分:0)

我通过将代码更改为此来解决了问题:

function listeners(){
    validation = new Validator();
    for(input in inputs){
        var el = $("#"+input+"");
        var i = inputs[input];
        (function(input, e){
            addEvent(e, "input", function(){
                data[input] = input.value = e.value;
                if(validation.fails(input)){
                    //Do something
                }
            })
        })(i, el)

    }
}