javascript jquery点击(记住旧值)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4343120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:06:11  来源:igfitidea点击:

jquery click (remember OLD VALUE)

javascriptjquery

提问by Dezigo

While I click on select element, I need to remember old value of the selected element.
This code works in FF,CHROME and OPERA instead of IE.

当我点击选择元素时,我需要记住所选元素的旧值。
此代码适用于 FF、CHROME 和 OPERA 而不是 IE。

  1. Remember the old value.
  2. Choose select option and replace old value remembered with a selected option!
  1. 记住旧值。
  2. 选择选择选项并用所选选项替换记住的旧值!

Code:

代码:

   $(document).ready(function() {
        $('#viewTableOrders tr td > select').click(function() {
          oldCurrentPath = $(this).val();
          oldId = $(this).attr('title');
          oldCurrentDate = $('#viewTableOrders tr#vagon' + oldId + '> td >input.input_ver1').val();
          dataAjax = {};
       });


        /* event on change path  */
        $('#viewTableOrders tr td > select').change(function() {
            //do something... + old value
        });  
    });

回答by Dr.Molle

It looks like the problem is that the 2nd click(choosing new option) will fire before the change-event.

看起来问题在于第二次点击(选择新选项)将在更改事件之前触发。

Try using focus instead of click:

尝试使用焦点而不是单击:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>Old value is:<span></span>
<script>
$(document).ready(
  function()
  {
    $('select').focus(function()
                      {
                          //store old value
                        $(this).data('oldValue',$(this).val());}
                      );
    $('select').change(function()
                       {
                          //do something
                        $('span').text($(this).data('oldValue'));
                          //trigger focus to set new oldValue
                        $(this).trigger('focus');
                       });

  }
);
</script>

(Example also uses data suggested by prodigitalson)

(示例还使用 prodigitalson 建议的数据)

回答by prodigitalson

I would use the datainfrastructure for this:

我会为此使用data基础设施:

$('#viewTableOrders tr td > select').click(function() {
  var old = {};
  old.currentPath = $(this).val();
  old.id = $(this).attr('title');
  old.currentDate = $('#viewTableOrders tr#vagon' + old.id + '> td >input.input_ver1').val();
  old.ajaxData = {};
  $(this).data('oldData', old);

}).change(function() {
  var oldData = $(this).data('oldData');
  if(typeof old == 'object'){
    // do stuff with oldData
  }      
});

回答by Matt

The question is a little old, but I think we also need to look at the key up/down events, otherwise using the cursor to change the select values doesn't cause the old value to get updated. This worked for me, based on @Dr.Molle's code:

问题有点老了,但我认为我们还需要查看键上/下事件,否则使用光标更改选择值不会导致旧值更新。这对我有用,基于@Dr.Molle 的代码:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>Old value is:<span></span>
    <script>
    $(document).ready(
      function()
      {
        $('select').focus(function()
          {
            //store old value
            $(this).data('oldValue',$(this).val());
          });
        $('select').change(function()
            {
              //do something
              $('span').text($(this).data('oldValue'));
              //trigger focus to set new oldValue
              $(this).trigger('focus');
            });
        $('select').keydown(function()
            {
            //store old value
            $(this).data('oldValue',$(this).val());
            });
        $('select').keydown(function()
            {
              //do something
              $('span').text($(this).data('oldValue'));
              //trigger focus to set new oldValue
              $(this).trigger('focus');
            });

      }
    );
    </script>