Programing

변경 전 선택 (드롭 다운) 값 얻기

lottogame 2020. 4. 23. 07:41
반응형

변경 전 선택 (드롭 다운) 값 얻기


내가 달성하고자하는 것은 <select>드롭 다운이 변경 될 때마다 변경하기 전에 드롭 다운 값을 원한다는 것입니다. 1.3.2 버전의 jQuery를 사용하고 on change 이벤트를 사용하고 있지만 내가 얻은 값은 변경 후입니다.

<select name="test">
<option value="stack">Stack</option>
<option value="overflow">Overflow</option>
<option value="my">My</option>
<option value="question">Question</option>
</select>

onchange 이벤트에서 스택으로 변경할 때 (즉, 스택으로 변경했을 때) 현재 My 옵션이 선택되어 있다고 가정 해 보겠습니다.이 경우 이전 값을 원합니다.

이것이 어떻게 달성 될 수 있습니까?

편집 : 내 경우에는 같은 페이지에 여러 개의 선택 상자가 있고 모든 항목에 동일한 항목이 적용되기를 원합니다. 또한 내 선택은 모두 ajax를 통해 페이지를로드 한 후에 삽입됩니다.


포커스 이벤트를 변경 이벤트와 결합하여 원하는 것을 달성하십시오.

(function () {
    var previous;

    $("select").on('focus', function () {
        // Store the current value on focus and on change
        previous = this.value;
    }).change(function() {
        // Do something with the previous value after the change
        alert(previous);

        // Make sure the previous value is updated
        previous = this.value;
    });
})();

실례 : http://jsfiddle.net/x5PKf/766


이것을 위해 전역 변수를 사용하지 마십시오-여기에 데이터에 이전 값을 저장하십시오 예는 다음과 같습니다 : http://jsbin.com/uqupu3/2/edit

심판 코드 :

$(document).ready(function(){
  var sel = $("#sel");
  sel.data("prev",sel.val());

  sel.change(function(data){
     var jqThis = $(this);
     alert(jqThis.data("prev"));
     jqThis.data("prev",jqThis.val());
  });
});

방금 페이지에 많은 선택이 있음을 보았습니다.이 방법은 각 선택에 대해 선택 값의 데이터에 이전 값을 저장하기 때문에 효과가 있습니다.


나는 Avi Pinto의 솔루션을 사용합니다. jquery.data()

초점을 사용하는 것은 유효한 해결책이 아닙니다. 처음에는 옵션을 변경할 때 작동하지만 해당 선택 요소를 유지 한 상태에서 "위"또는 "아래"키를 누릅니다. 포커스 이벤트를 다시 거치지 않습니다.

따라서 솔루션은 다음과 같이 보입니다.

//set the pre data, usually needed after you initialize the select element
$('mySelect').data('pre', $(this).val());

$('mySelect').change(function(e){
    var before_change = $(this).data('pre');//get the pre data
    //Do your work here
    $(this).data('pre', $(this).val());//update the pre data
})

손으로 값을 추적하십시오.

var selects = jQuery("select.track_me");

selects.each(function (i, element) {
  var select = jQuery(element);
  var previousValue = select.val();
  select.bind("change", function () {
    var currentValue = select.val();

    // Use currentValue and previousValue
    // ...

    previousValue = currentValue;
  });
});

 $("#dropdownId").on('focus', function () {
    var ddl = $(this);
    ddl.data('previous', ddl.val());
}).on('change', function () {
    var ddl = $(this);
    var previous = ddl.data('previous');
    ddl.data('previous', ddl.val());
});

이벤트 "live"를 사용하고 있는데 솔루션은 기본적으로 Dimitiar와 유사하지만 "focus"를 사용하는 대신 "click"이 트리거 될 때 이전 값이 저장됩니다.

var previous = "initial prev value";
$("select").live('click', function () {
        //update previous value
        previous = $(this).val();
    }).change(function() {
        alert(previous); //I have previous value 
    });

각도 시계 유형 인터페이스와 함께 사용자 정의 jQuery 이벤트를 사용하는 것은 어떻습니까?

// adds a custom jQuery event which gives the previous and current values of an input on change
(function ($) {
    // new event type tl_change
    jQuery.event.special.tl_change = {
        add: function (handleObj) {
            // use mousedown and touchstart so that if you stay focused on the
            // element and keep changing it, it continues to update the prev val
            $(this)
                .on('mousedown.tl_change touchstart.tl_change', handleObj.selector, focusHandler)
                .on('change.tl_change', handleObj.selector, function (e) {
                // use an anonymous funciton here so we have access to the
                // original handle object to call the handler with our args
                var $el = $(this);
                // call our handle function, passing in the event, the previous and current vals
                // override the change event name to our name
                e.type = "tl_change";
                handleObj.handler.apply($el, [e, $el.data('tl-previous-val'), $el.val()]);
            });
        },
        remove: function (handleObj) {
            $(this)
                .off('mousedown.tl_change touchstart.tl_change', handleObj.selector, focusHandler)
                .off('change.tl_change', handleObj.selector)
                .removeData('tl-previous-val');
        }
    };

    // on focus lets set the previous value of the element to a data attr
    function focusHandler(e) {
        var $el = $(this);
        $el.data('tl-previous-val', $el.val());
    }
})(jQuery);

// usage
$('.some-element').on('tl_change', '.delegate-maybe', function (e, prev, current) {
    console.log(e);         // regular event object
    console.log(prev);      // previous value of input (before change)
    console.log(current);   // current value of input (after change)
    console.log(this);      // element
});

드롭 다운 '변경시'액션 함수를 작성하기 전에 현재 선택된 드롭 다운 값을 선택한 jquery로 전역 변수에 유지하십시오. 함수에서 이전 값을 설정하려면 전역 변수를 사용할 수 있습니다.

//global variable
var previousValue=$("#dropDownList").val();
$("#dropDownList").change(function () {
BootstrapDialog.confirm(' Are you sure you want to continue?',
  function (result) {
  if (result) {
     return true;
  } else {
      $("#dropDownList").val(previousValue).trigger('chosen:updated');  
     return false;
         }
  });
});

나는 이것이 오래된 실이라는 것을 알고 있지만 조금 더 추가 할 수 있다고 생각했습니다. 제 경우에는 텍스트, val 및 기타 데이터 속성을 전달하고 싶었습니다. 이 경우 전체 옵션을 val이 아닌 prev 값으로 저장하는 것이 좋습니다.

아래 예제 코드 :

var $sel = $('your select');
$sel.data("prevSel", $sel.clone());
$sel.on('change', function () {
    //grab previous select
    var prevSel = $(this).data("prevSel");

    //do what you want with the previous select
    var prevVal = prevSel.val();
    var prevText = prevSel.text();
    alert("option value - " + prevVal + " option text - " + prevText)

    //reset prev val        
    $(this).data("prevSel", $(this).clone());
});

편집하다:

요소에 .clone ()을 추가하는 것을 잊었습니다. 값을 되 찾을 때 이전 선택이 아닌 선택의 새 사본을 가져옵니다. clone () 메소드를 사용하면 인스턴스 대신 선택 사본을 저장합니다.


음, 현재 선택된 값을 저장하지 않는 이유는 무엇입니까? 선택한 항목이 변경되면 이전 값이 저장됩니까? (원하는대로 다시 업데이트 할 수 있습니다)


다음 코드를 사용하여 테스트했으며 작동합니다.

var prev_val;
$('.dropdown').focus(function() {
    prev_val = $(this).val();
}).change(function(){
            $(this).unbind('focus');
            var conf = confirm('Are you sure want to change status ?');

            if(conf == true){
                //your code
            }
            else{
                $(this).val(prev_val);
                $(this).bind('focus');
                return false;
            }
});

(function() {

    var value = $('[name=request_status]').change(function() {
        if (confirm('You are about to update the status of this request, please confirm')) {
            $(this).closest('form').submit(); // submit the form
        }else {
            $(this).val(value); // set the value back
        }
    }).val();
})();

이 문제를 해결하기 위해 다른 옵션을 제공하고 싶습니다. 위에서 제안한 솔루션이 내 시나리오를 해결하지 못했기 때문에.

(function()
    {
      // Initialize the previous-attribute
      var selects = $('select');
      selects.data('previous', selects.val());

      // Listen on the body for changes to selects
      $('body').on('change', 'select',
        function()
        {
          $(this).data('previous', $(this).val());
        }
      );
    }
)();

이것은 jQuery를 사용하여 def를 사용합니다. 여기에 의존하지만 순수한 자바 스크립트에서 작동하도록 조정할 수 있습니다. (본인에 리스너를 추가하고 원래 대상이 선택, 실행 기능인지 확인하십시오.)

바디에 변경 리스너를 연결하면 선택에 대한 특정 리스너 이후에 이것이 발생하는지 확인할 수 있습니다 . 그렇지 않으면 데이터를 읽기 전에 'data-previous'값을 덮어 씁니다.

이것은 물론 이전 값과 체크 값에 별도의 리스너를 사용하는 것을 선호한다고 가정합니다. 단일 책임 패턴에 잘 맞습니다.

참고 : 이렇게하면이 '이전'기능이 모든 선택에 추가되므로 필요한 경우 선택기를 미세 조정해야합니다.


이것은 @thisisboris 답변의 개선입니다. 데이터에 현재 값을 추가하므로 코드는 현재 값으로 설정된 변수가 변경되는시기를 제어 할 수 있습니다.

(function()
{
    // Initialize the previous-attribute
    var selects = $( 'select' );
    $.each( selects, function( index, myValue ) {
        $( myValue ).data( 'mgc-previous', myValue.value );
        $( myValue ).data( 'mgc-current', myValue.value );  
    });

    // Listen on the body for changes to selects
    $('body').on('change', 'select',
        function()
        {
            alert('I am a body alert');
            $(this).data('mgc-previous', $(this).data( 'mgc-current' ) );
            $(this).data('mgc-current', $(this).val() );
        }
    );
})();

최고의 솔루션 :

$('select').on('selectric-before-change', function (event, element, selectric) {
    var current = element.state.currValue; // index of current value before select a new one
    var selected = element.state.selectedIdx; // index of value that will be selected

    // choose what you need
    console.log(element.items[current].value);
    console.log(element.items[current].text);
    console.log(element.items[current].slug);
});

원하는 결과를 얻는 몇 가지 방법이 있습니다.

요소가 이전 값을 보유하게하려면 속성 'previousValue'를 추가하십시오.

<select id="mySelect" previousValue=""></select>

초기화되면 'previousValue'를 속성으로 사용할 수 있습니다. JS에서이 선택의 이전 값에 액세스하려면 다음을 선택하십시오.

$("#mySelect").change(function() {console.log($(this).attr('previousValue'));.....; $(this).attr('previousValue', this.value);}

'previousValue'사용을 완료 한 후 속성을 현재 값으로 업데이트하십시오.


선택에 따라 다른 div를 공개해야했습니다.

이것이 jquery 및 es6 구문으로 수행하는 방법입니다

HTML

<select class="reveal">
    <option disabled selected value>Select option</option>
    <option value="value1" data-target="#target-1" >Option 1</option>
    <option value="value2" data-target="#target-2" >Option 2</option>
</select>
<div id="target-1" style="display: none">
    option 1
</div>
<div id="target-2" style="display: none">
    option 2
</div>

JS

$('select.reveal').each((i, element)=>{
    //create reference variable 
    let $option = $('option:selected', element)
    $(element).on('change', event => {
        //get the current select element
        let selector = event.currentTarget
        //hide previously selected target
        if(typeof $option.data('target') !== 'undefined'){
            $($option.data('target')).hide()
        }
        //set new target id
        $option = $('option:selected', selector)
        //show new target
        if(typeof $option.data('target') !== 'undefined'){
            $($option.data('target')).show()
        }
    })
})

참고 URL : https://stackoverflow.com/questions/4076770/getting-value-of-select-dropdown-before-change

반응형