0

在我的 Rails 应用程序中,我正在使用 class 监听一堆输入字段regex-field。我正在听一些咖啡脚本代码。每次我在其中一个输入字段中输入内容时,resetRegex都会触发该方法。正如您在下面看到的,我想要regexField.attr "regex". 但无论我输入哪个字段,它似乎总是相同的值。知道我做错了什么吗?

  $ ->
    createSubTable(data, email_type_id)
    resetRegex()

    $(document).ajaxSuccess (event, request, settings) ->
      resetRegex()

  resetRegex = ->

    regexField = $(".regex-field")
    regexField.off "keyup"
    regexField.keyup (event) ->
      email_type = regexField.attr "email-type-id"

      console.log regexField                 # => [input#order-id-regex.regex-field, input#name-regex.regex-field, input#mail-address-regex.regex-field, prevObject: jQuery.fn.jQuery.init[1], context: document, selector: ".regex-field", jquery: "1.9.1", constructor: function…]
      console.log regexField.attr "regex"    # => order_id_regex

      $.ajax
        type: "get"
        dataType: "json"
        data:
          value: regexField[0].value
          database_field: regexField.attr "regex"
        url: "/api/email_type/"+email_type+"/save_regex"

        success: (data, status, xhr) ->

        error: (xhr, textStatus, errorThrown) ->


  createSubTable = (data, email_type_id) ->

    jobsContainer = $("#jobs")

    jobsTable2 = $("<table />").addClass("table")
    jobsContainer.append(jobsTable2)

    jobsThead2 = $("<thead />")
    jobsHeadTr2 = $("<tr />")

    jobsTh2 = $("<th />").text("Email body")
    jobsHeadTr2.append(jobsTh2)

    jobsThead2.append(jobsHeadTr2)
    jobsTable2.append(jobsThead2)

    jobsTbody2 = $("<tbody />")

    lineNumber = 0

    data[0].extra.test_return.body.map (line) ->

        jobsBodyTrLoop = $("<tr />")
        jobsTdLoop4 = $("<td />")

        if data[0].extra.chosen_fields.order_id_line is lineNumber
            typeInput1 = $("<input />").addClass("regex-field").attr("email-type-id", email_type_id).attr("id", "order-id-regex").attr("regex", "order_id_regex")
            jobsTdLoop4.append(typeInput1)
        else if data[0].extra.chosen_fields.mail_address_line is lineNumber
            typeInput1 = $("<input />").addClass("regex-field").attr("email-type-id", email_type_id).attr("id", "mail-address-regex").attr("regex", "mail_address_regex")
            jobsTdLoop4.append(typeInput1)
        else if data[0].extra.chosen_fields.name_line is lineNumber
            typeInput1 = $("<input />").addClass("regex-field").attr("email-type-id", email_type_id).attr("id", "name-regex").attr("regex", "name_regex")
            jobsTdLoop4.append(typeInput1)

        jobsBodyTrLoop.append(jobsTdLoop4)

        jobsTbody2.append(jobsBodyTrLoop)

        lineNumber++

    jobsTable2.append(jobsTbody2)
4

1 回答 1

0

您需要使用$(this)来引用触发事件的元素。

regexField.keyup (event) ->
    email_type = $(this).attr "email-type-id"
    console.log $(this)  # => should output the field that triggered the event                 
    console.log $(this).attr "regex"    # => the attr "regex" of the field
于 2013-07-21T19:24:40.723 回答