简体   繁体   English

jQuery .click更改元素引用的变量?

[英]Jquery .click on variable with changing element reference?

I have this JavaScript code. 我有这个JavaScript代码。 It seems to be working in every way but one. 除了一种以外,它似乎在以各种方式起作用。 This script is for a small timeclock application. 该脚本适用于小型时钟应用程序。 On the main page, some PHP decides whether to display a "Start" button, a "Stop" button, or a textarea to note the work done during the time period recorded based upon some information in the database. 在主页上,一些PHP根据数据库中的某些信息决定是否显示“开始”按钮,“停止”按钮或文本区域,以记录在记录的时间段内完成的工作。

This JS code is intended to send an AJAX request to a PHP file which manipulates the DB based on which of the inputs is currently visible. 该JS代码旨在将AJAX请求发送到PHP文件,该文件根据当前可见的输入来操纵数据库。 If the PHP script doesn't return an error, the JS should remove the current input from the DOM and add the input associated with the next step in the cycle to the DOM. 如果PHP脚本未返回错误,则JS应从DOM中删除当前输入,并将与循环中的下一步相关联的输入添加到DOM。

I believe the problem here is that the .click() function of jQuery is not triggered after the initial element reference of currentSubmit has changed. 我相信这里的问题是在currentSubmit的初始元素引用已更改后,jQuery的.click()函数未触发。

I could be wrong, but whatever the issue is, how do I get around it? 我可能是错的,但是无论出什么问题,我该如何解决?

if($('input[name="timeclock_input"]').length == 0) {
  var currentSubmit = $('input[name="submit_record"]');
} else {
  var currentSubmit = $('input[name="timeclock_input"]');
}
var inputValueArray = [
  "Start Timeclock",
  "Stop Timeclock",
  "Add Record"
];
currentSubmit.click(function() {
  if (currentSubmit.val() == inputValueArray[0]) {
    $.post( "php/record.php", { action: "start" })
      .done(function( data ) {
    if (data == "success") {
      $('input[name="timeclock_input"]').replaceWith('<input type="button" name="timeclock_input" value="Stop Timeclock" />');
      currentSubmit = $('input[name="timeclock_input"]');
    } else {
      alert(data);
    }
      });
  } else if (currentSubmit.val() == inputValueArray[1]) {
    $.post( "php/record.php", { action: "stop" })
      .done(function( data ) {
    if (data == "success") {
      $('input[name="timeclock_input"]').replaceWith('<div id="record-container"><textarea name="timeclock_input" placeholder="Description of Work Completed"></textarea><br /><input type="button" name="submit_record" value="Add Record" /></div>');
      currentSubmit = $('input[name="submit_record"]');
    } else {
      alert(data);
    }
      });
  } else if (currentSubmit.val() == inputValueArray[2]) {
    $.post( "php/record.php", { action: "record" })
      .done(function( data ) {
    if (data == "success") {
      $('#record-container').replaceWith('<input type="button" name="timeclock_input" value="Stop Timeclock" />');
      currentSubmit = $('input[name="timeclock_input"]');
    } else {
      alert(data);
    }
      });
  }
});

I found the answer to my question. 我找到了问题的答案。 The solution is to use event delegation like this... 解决方案是使用这样的事件委托...

$('#input-container').on( "click", "input", function() {
     Other Code Here...
});

You have 3 different buttons that do three different things. 您有3个不同的按钮,可以执行三种不同的操作。 I would assign a separate $.click(function(){...}) for each button. 我将为每个按钮分配一个单独的$.click(function(){...}) The default(first) button should be visible ( $.show() ) and the other two hidden ( $.hide() ) on page load. 默认(第一个)按钮在页面加载时应该是可见的( $.show() )和其他两个隐藏的( $.hide() )。 Then in each if (data == "success"){...} section $.hide() the current button and $.show() the next button. 然后在每个if (data == "success"){...}部分的$.hide() ,当前按钮和$.show()下一个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM