English 中文(简体)
在变化发生之前选择html的价值
原标题:Get value of html select before being changed in change event

在从html选择处理“变革”活动时,我想获得最后选定的价值/指数。

页: 1

$( #mySelect ).live( change , function(event){
       var idx = $(this).prop( selectedIndex );
});

补贴在改变事件发生后保持价值。 我需要事先确定的事件触发点的价值。

最佳回答

如果你们有多种选择的话,就象这样:

var select = $( #mySelect );
select 
    .data("lastIndex", select[0].selectedIndex)
    .live( change , function(event){
        var idx = this.selectedIndex;
        var prevIdx = $(this).data("lastIndex");

        // Alert prev and new index
        alert(prevIds + " - " + idx);

        // Alert prev value and new value
        alert($(this).find("option").eq(prevIdx) + " - " + $(this).find("option").eq(idx));

        // Set new index as old index
        $(this).data("lastIndex", idx);
    });
问题回答

我将储存不同变量的以往价值,并以人工更新。

// what is the value when <select> was just loaded?
var previous_value = $( #mySelect ).prop( selectedIndex );

$( #mySelect ).live( change , function(event){
       var idx = $(this).prop( selectedIndex );
       // at this point previous_value has the previous value and idx - current
       // ... do something here ...
       // updating previous_value for the next time
       previous_value = idx;
});

你们能够利用你处理事件职能之外的可弥补的范围,追踪价值,例如:

var mySelect = $( #mySelect ),
    previousValue = mySelect.val();

mySelect.live( change , function() {

    alert( mySelect.val() ); // alerts current value
    alert( previousValue ); // alerts previous value

    previousValue = mySelect.val(); // save so it can be referenced next time

});

你可以尝试像这样的东西。

var lastSeelctedlist = "";
$( #mySelect ).live( change , function(event){
       lastSeelctedlist  = $(this).val();
// you rest of code
});

如果是你正确的话,那是纯javascript的解决办法。

function getLastSelected(e)
   {
    var s = e;
    var optionsSelected = [];
    for(var i =0;i<s.options.length;i++)
    {
     if(s.options[i].selected == true)
      {
        optionsSelected[optionsSelected.length] = {Value :  s.options[i].value,Text:s.options[i].text,Index : i};
      }
    }

    var lastSelected =((optionsSelected.length >0)?(optionsSelected[optionsSelected.length - 1]):(null)) ;
    return lastSelected; // or do what you have to do here 
//put it in custom attribute or what ever 
   }

this function parameter is select tag id and return object is contains 3 properties Value,Text,Index

呼吁举行变革活动

onchange = "getLastSelected(this)"

关于





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签