如何将元素的键值存储在Click?

时间:2017-04-29 16:23:33

标签: javascript firebase-realtime-database

       var jobID = {};

      function gotData(data) { 
              var date = Today;

              var container = document.getElementById('pos_1'); 
              var container2 = document.getElementById('jobapp'); 

              data.forEach(function(jobSnap) { // loop over all jobs
                var key = jobSnap.key;
                var job = jobSnap.val();
                var newCard = `
                  <li class="pos-card" id="${key}">
                        <div class="content">
                          <div class="title new">${job.JobTitle}</div>
                          <div class="dept">Customer Service</div>
                          <div class="date">${date}</div>
                          <div class="refer">Apply</div>
                        </div>
                        <ul class="desc">
                          <li>${job.JobSummary}</li>
                        </ul>
                  </li>
              `;
              container.innerHTML += newCard;
               jobID.value = key;
              console.log(key);
              })
            }

上面的循环函数显示这样的firebase数据库子项中的所有条目,并在右侧控制台中列出了各自的firebase数据库密钥。 enter image description here

我需要做的是捕获单击的相应作业的键,以便我可以在下面的函数中使用它来保存作业应用程序在所单击的键下的所有条目。我试图将jobId声明为全局变量,然后为其分配键值,但我无法在另一个函数中使用它。如何捕获单击作业的键,然后在另一个函数中将其用作JobId?例如,我如何在下面的函数中使用它

       function newApplication() {

      var database = firebase.database();
      var applicant_Name = document.getElementById('name').value;
      var applicant_Number = document.getElementById('phone').value; 
      var applicant_email = document.getElementById('email').value; 
      var AuthorId = firebase.auth().currentUser.uid;
      var cover_letter = document.getElementById('cover_letter').value;

      console.log(key);
       var JobId.value = key;

      var postData = {
              ApplicantName: applicant_Name,
              ApplicantNumber: applicant_Number,
              Applicantemail: applicant_email, 
              Author: AuthorId,
              Cover_letter: cover_letter,
          };

      var newPostKey = firebase.database().ref().child('Applications').push().key;    

      var updates = {};


          updates['/Applications/' + newPostKey] = postData;
          updates[ JobId + '/Applications/' + newPostKey] = postData;

      return firebase.database().ref().update(updates); 

  }

控制台在newApplication函数中显示错误消息“key is not defined”。

更新:这是我的点击处理程序

 $('.container').on('click', '.refer', function(e){
 alert(jobID.value );
 $('.positions').addClass('fadeOut');
 $('.refer-card').addClass('fade');
  $('.return').fadeIn('fast');
 })

1 个答案:

答案 0 :(得分:2)

如果点击# Create some variables. v1 = tf.Variable(..., name="v1") v2 = tf.Variable(..., name="v2") ... # Add ops to save and restore only 'v2' using the name "my_v2" saver = tf.train.Saver({"my_v2": v2}) # Use the saver object normally after that. ... 元素,则在其点击处理程序中,.refer给出id="${key}"的相应元素。

因此您似乎想要:

$(this).closest("li")

不清楚如何调用var JobId = $(this).closest("li").prop('id'); ,但看起来您需要将newApplication()作为参数传递。