变更事件未被识别?
问题描述:
我有这样的代码,其寻找一个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/
谁能帮助?
答
你真正的问题是这样的:
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是不是装上我的网站上该链接的每个页面加载,所以如果它不工作的第一次,重新加载页面具有完全加载后。
答
在拨弄你使用一些自定义select
(一个div)的“活”网站
$('.outtaHere ul li a').click(function() {
// do you stuff to show the elements
});
使用select
我不知道你是否可以更改自定义的代码选择,但它将删除内联JavaScript代码更清洁。
答
//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
}
想法是,当选择下拉选项时,它会显示目的地内的相对隐藏div。 – Andy 2011-06-11 13:40:24
你不实际使用'#regionselect'元素。有一个美化下拉框('#sarea8')被插入到文档中,而'#regionselect'放逐300到左边。 – lonesomeday 2011-06-11 13:42:01
如何在代码更改时运行代码? – Andy 2011-06-11 13:44:42