【发布时间】:2016-09-15 21:20:17
【问题描述】:
我一直想知道并且找不到解决方案为什么操作 $("#locationid3").change(第 85 行)不起作用,即使它与 $("#locationid").change 和 $("#locationid2").change (它上面的行)。主要区别在于: - #locationid 和 #locationid2 直接放置在 html-form 中并且始终可见 - #locationid3 放置在 html-form 之外的隐藏 div 中,只有在 product-dropdown 中选择“prod2”时才会显示
在表单外创建隐藏 div 的原因是可以通过更改产品下拉菜单重新加载表单内的字段集。通过更改产品,所有字段集将使用“hideFieldsets()”方法隐藏,相应的字段集(使用帐户、产品、data-id 和数据位置)将使用“showFieldset()”方法显示。
我已将代码放入jsfiddle
$(document).ready(function(){
$("#accountid").change(function() {
var $dropdown = $(this);
var accounts = {
"id_1":{"products":[{"id":"1","val":"prod1"},{"id":"2","val":"prod2"}],"locations":[{"id":"4","val":"loc1"},{"id":"1","val":"loc2"}],}
};
var key = $dropdown.val();
var products = {};
var locations = {};
var locations2 = {};
var locations3 = {};
switch(key) {
case 'id_1' :products = accounts.id_1.products;locations = accounts.id_1.locations;locations2 = locations;locations3 = locations;break;
}
var $productid = $("#productid");
$productid.empty();
$.each(products, function(index, value) {
var option = $('<option></option>').val(value.id).text(value.val);
$productid.append(option);
});
$('#productid').val();
$productid.trigger("change");
var $locationid = $("#locationid");
$locationid.empty();
$.each(locations, function(index, value) {
var option = $('<option></option>').val(value.id).text(value.val);
$locationid.append(option);
});
var $locationid2 = $("#locationid2");
$locationid2.empty();
$.each(locations2, function(index, value) {
var option = $('<option></option>').val(value.id).text(value.val);
$locationid2.append(option);
});
var $locationid3 = $("#locationid3");
$locationid3.empty();
$.each(locations3, function(index, value) {
var option = $('<option></option>').val(value.id).text(value.val);
$locationid3.append(option);
});
});
$("#productid").change(function() {
var possibleFieldsets = {
"id_1_1" : ["id_1_1__ test ","id_1_1__ test2 "],
"id_1_2" : ["id_1_2__ test ","id_1_2__ test2 "],
};
hideFieldsets();
var selectedAccProd = $("#accountid option:selected").val()+"_"+$("#productid option:selected").val();
showFieldsets(possibleFieldsets[selectedAccProd]);
});
$("#locationid").change(function() {
readLocationData(this.value);
});
$("#locationid2").change(function() {
readLocationData(this.value);
});
$("#locationid3").change(function() {
readLocationData(this.value);
});
var allFieldset = {};
$('#allFieldsets').children().each(function(index,fieldsetElement){
var $fieldsetElement = $(fieldsetElement);
allFieldset[$fieldsetElement.attr("data-id")] = $fieldsetElement;
});
$('#allFieldsets').remove();
function hideFieldsets(){
for(var key in allFieldset){
var $div = $('fieldset[data-id="'+key+'"]').parent();
if($div.children().length > 0){
allFieldset[key] = $div.children();
$div.empty();
}
$div.hide();
}
}
function showFieldsets(fieldsetArray){
$.each(fieldsetArray, function(index, element){
var $div = $('div[data-position="'+element.split('__')[1]+'"]');
$div.append(allFieldset[element]);
$div.show();
});
}
function readOrderData(orderId){
window.alert("reading data");
}
function readLocationData(locationId){
window.alert("location changed");
}
$("#accountid").trigger("change");
});
$isUnread = false;
我非常感谢有关如何使#locationsid3 change() 起作用的任何线索/建议。谢谢!
【问题讨论】:
-
使用
$("#locationid3").on("change",function() { readLocationData(this.value); });或$("document").on("change", "#locationid3",function() { readLocationData(this.value);});