2011-06-11 86 views
0

我有這樣的代碼,其尋找一個onchange事件:變更事件未被識別?

//Create destination switch 
    $('#regionselect').change(function(){ 
     var selected = $(this).find(':selected'); 
     $('.optionvalue').fadeOut(function(){ 
      $('.optionvalue').html(selected.html()).fadeIn() 
       .attr('class', 'optionvalue '+selected.val()); 
     }); 
     var count = $('.countrylist').length; 
     $('.countrylist').slideUp(function(){ 
      if(!--count) { 
       $('.'+selected.val()).slideDown(); 
      }  
     }); 
    }); 

它完美的位置: http://jsfiddle.net/XfPv7/1/

誰能幫助?

+0

想法是,當選擇下拉選項時,它會顯示目的地內的相對隱藏div。 – Andy 2011-06-11 13:40:24

+1

你不實際使用'#regionselect'元素。有一個美化下拉框('#sarea8')被插入到文檔中,而'#regionselect'放逐300到左邊。 – lonesomeday 2011-06-11 13:42:01

+0

如何在代碼更改時運行代碼? – Andy 2011-06-11 13:44:42

回答

0

你真正的問題是這樣的:

http://www.thestc.co.uk/assets/js/form.js

發現這裏的腳本包含的功能populateSelectOptions()selectMe()showOptions(),一起用來打敗你真正的選擇onChange事件處理程序,因爲你沒有與互動你真正的選擇。

如果你殺了選擇替換腳本,它工作正常。請參閱:http://jfcoder.com/test/customselect.html

編輯

這裏的kind've蠻力的方式來解決這個問題。

function setDestOpts(dest) { 
    reg = $('#regionselect'); 
    reg.val(dest); 
    reg.trigger('change'); 
    $('#optionsDiv8').removeClass('optionsDivVisible').addClass('optionsDivInvisible'); 
} 
var checkDest = function() { 
    if ($('#optionsDiv8').length == 0) return; 
    clearInterval(destinterval); 
    c_opt = 0; 
    $('#optionsDiv8 ul li a').each(function(c_opt){ 
     reg = $('#regionselect option'); 
     href = "javascript:setDestOpts('" + reg.eq(c_opt).val() + "');"; 
     $(this).attr('href', href); 
     c_opt++; 
    }); 
} 

var destinterval = setInterval(function(){checkDest()}, 500); 

http://jfcoder.com/test/customselect.html

這一切確實是改變的Javascript注入A.HREF,這是問題的根源。這模擬了值的變化,然後觸發onChange事件。

注意,出於某種原因,jQuery是不是裝上我的網站上該鏈接的每個頁面加載,所以如果它不工作的第一次,重新加載頁面具有完全加載後。

+0

有沒有一種方法可以同時使用?我真的想保持樣式 – Andy 2011-06-11 14:09:59

+0

不是沒有完全重寫腳本以允許不同的功能 – 2011-06-11 14:13:18

+0

哦親愛的:(感謝您的幫助。 – Andy 2011-06-11 14:22:33

0

在撥弄你使用一些自定義select(一個div)的「活」網站

$('.outtaHere ul li a').click(function() { 
// do you stuff to show the elements 
}); 

使用select我不知道你是否可以更改自定義的代碼選擇,但它將刪除內聯JavaScript代碼更清潔。

+0

即使我把它改到div它不會使任何區別:( – Andy 2011-06-11 13:52:36

+0

你能或許與設置完全相同的代碼小提琴這樣我就可以修復它有 – PeeHaa 2011-06-11 13:56:39

+0

是的肯定:這裏http://jsfiddle.net/ M3kjv/ – Andy 2011-06-11 13:59:10

1
//This worked for me - I appear to have inherited similar jquery custom select code 

function onChange() 
{ 
    var el=document.getElementById("selectid"); 

    //el.options[] is your array of options 
    if (el.options[0].selected==true) 
     //Some code 
}