首页 \ 问答 \ 选中JQuery Bootstrap selectpicker on('change')设置(JQuery Bootstrap selectpicker on('change') set selected)

选中JQuery Bootstrap selectpicker on('change')设置(JQuery Bootstrap selectpicker on('change') set selected)

我正在尝试禁用并默认 - 根据另一个选择的值选择<select> 。 当我第一次在<select> choixVarX中选择一个选项(例如“Pop”)时, <select> parametresX正是我想要它做的:禁用前两个<optgroup>和默认选择“pasCentre”和“noDispersion”。 但是,当我将choixVarX更改为另一个不触发我想要的行为(例如“PropRetraite”)然后再将其更改为触发我想要的行为(例如“FoyersImposes”或“FoyersFiscaux”)时,第二个<select> parametresX表现<optgroup>奇怪:两个第一个<optgroup>被禁用,但“pasCentre”和“noDispersion”不是默认选择...任何线索?

<select class="selectpicker" name="choixVarX" id="choixVarX" size="1" onChange="populateMap('#DDDDC2','#000000',listeComparaison);">
        <option value="Pop">Population</option>
        <option value="FoyersImposes">Nombre de foyers imposés</option>
        <option value="FoyersFiscaux">Nombre de foyers fiscaux</option>
        <option value="PropRetraite">Part des retraites dans les revenus</option>
        <option value="PropFoyersImposes">Proportion de foyers imposés</option>
        <option value="RevMoyen">Revenu moyen</option>
      </optgroup>
    </select>
    <select class="selectpicker" multiple name="parametresX[]" id="parametresX" size="1" data-width="150px" onChange="populateMap('#DDDDC2','#000000',listeComparaison);" data-max-options="3">
      <optgroup label="Centrage des données" data-max-options="1" id="centrageX">
        <option value="median">Médiane des communes</option>
        <option value="moyenne">Moyenne des communes</option>
        <option value="pasCentre">Ne pas centrer</option>
      </optgroup>
      <optgroup label="Dispersion" id="dispersionX" data-max-options="1">
        <option value="minmax">Min-Max</option>
        <option value="ecarttype">Ecart-type</option>
        <option value="noDispersion">Aucun</option>
      </optgroup>
      <optgroup label="Echelle" id="echelleX" data-max-options="1">
        <option value="echelleComplete">Echelle étendue</option>
        <option value="echelleAdaptee">Echelle réduite</option>
      </optgroup>
    </select>

$('#choixVarX').on('change',function(){
  if($('#choixVarX').val()=="FoyersImposes" || $('#choixVarX').val()=="FoyersFiscaux" || $('#choixVarX').val()=="Pop"){
    $('.selectpicker').selectpicker('refresh');
    $('#centrageX option[value="median"]').attr('selected',false);
    $('#centrageX option[value="moyenne"]').attr('selected',false);    
    $('#centrageX option[value="pasCentre"]').attr('selected',true);
    $('#dispersionX option[value="minmax"]').attr('selected',false);
    $('#dispersionX option[value="ecarttype"]').attr('selected',false);
    $('#dispersionX option[value="noDispersion"]').attr('selected',true);
    $('#centrageX').attr('disabled',true);
    $('#dispersionX').attr('disabled',true);
    $('.selectpicker').selectpicker('refresh');
  }else{
    $('#centrageX').attr('disabled',false);
    $('#dispersionX').attr('disabled',false);
    $('.selectpicker').selectpicker('refresh');
  }
});

I'm trying to disable and default-select a <select> based on the value on another select. When I first select one option in <select> choixVarX (e.g. "Pop"), <select> parametresX does exactly what I want it to do: disable the first two <optgroup> and default-select "pasCentre" and "noDispersion". However, when I change choixVarX to another one not triggering the behavior I want (e.g. "PropRetraite") and then change it again to one triggering the behavior I want (e.g. "FoyersImposes" or "FoyersFiscaux"), the second <select> parametresX behaves weirdly: the two first <optgroup> are disabled, but "pasCentre" and "noDispersion" are NOT default-selected... Any clue?

<select class="selectpicker" name="choixVarX" id="choixVarX" size="1" onChange="populateMap('#DDDDC2','#000000',listeComparaison);">
        <option value="Pop">Population</option>
        <option value="FoyersImposes">Nombre de foyers imposés</option>
        <option value="FoyersFiscaux">Nombre de foyers fiscaux</option>
        <option value="PropRetraite">Part des retraites dans les revenus</option>
        <option value="PropFoyersImposes">Proportion de foyers imposés</option>
        <option value="RevMoyen">Revenu moyen</option>
      </optgroup>
    </select>
    <select class="selectpicker" multiple name="parametresX[]" id="parametresX" size="1" data-width="150px" onChange="populateMap('#DDDDC2','#000000',listeComparaison);" data-max-options="3">
      <optgroup label="Centrage des données" data-max-options="1" id="centrageX">
        <option value="median">Médiane des communes</option>
        <option value="moyenne">Moyenne des communes</option>
        <option value="pasCentre">Ne pas centrer</option>
      </optgroup>
      <optgroup label="Dispersion" id="dispersionX" data-max-options="1">
        <option value="minmax">Min-Max</option>
        <option value="ecarttype">Ecart-type</option>
        <option value="noDispersion">Aucun</option>
      </optgroup>
      <optgroup label="Echelle" id="echelleX" data-max-options="1">
        <option value="echelleComplete">Echelle étendue</option>
        <option value="echelleAdaptee">Echelle réduite</option>
      </optgroup>
    </select>

$('#choixVarX').on('change',function(){
  if($('#choixVarX').val()=="FoyersImposes" || $('#choixVarX').val()=="FoyersFiscaux" || $('#choixVarX').val()=="Pop"){
    $('.selectpicker').selectpicker('refresh');
    $('#centrageX option[value="median"]').attr('selected',false);
    $('#centrageX option[value="moyenne"]').attr('selected',false);    
    $('#centrageX option[value="pasCentre"]').attr('selected',true);
    $('#dispersionX option[value="minmax"]').attr('selected',false);
    $('#dispersionX option[value="ecarttype"]').attr('selected',false);
    $('#dispersionX option[value="noDispersion"]').attr('selected',true);
    $('#centrageX').attr('disabled',true);
    $('#dispersionX').attr('disabled',true);
    $('.selectpicker').selectpicker('refresh');
  }else{
    $('#centrageX').attr('disabled',false);
    $('#dispersionX').attr('disabled',false);
    $('.selectpicker').selectpicker('refresh');
  }
});

原文:https://stackoverflow.com/questions/33730450
更新时间:2023-01-29 14:01

最满意答案

你有2个问题。 我看不到你在做什么,但A.link元素的大小只受视口高度的控制。 因此,如果视口高而窄,则链接很大,并且根本无法将它们全部拟合。

第二个问题是,如果无法放置链接,您的代码将永远尝试。 计时器永远不会触发,因为主脚本仍在忙着运行。 而不仅仅是做

while (!success)

最好做一些有限的尝试:

var success = false;
for (var attempt = 0; !success && attempt < 50; attempt++)
{
     ....
}

通过限制尝试次数,即使链接重叠,它也会停止冻结。 然后你可以删除冻结和计时器。 当尝试次数增加时,更好的是引入容差 - 因此允许它们重叠一点。


You have 2 problems. I don't see where you are doing it, but the size of your A.link elements is being controlled only by the height of the viewport. So if the viewport is tall and narrow, then the links are large, and it's simply impossible to fit them all.

The second problem is that if the links can't be placed your code just keeps trying forever. The timer will never fire because the main script is still busy running. Rather than just do

while (!success)

it would be better to do a limited number of attempts:

var success = false;
for (var attempt = 0; !success && attempt < 50; attempt++)
{
     ....
}

By limiting the number of tries it will stop it freezing, even if the links are left overlapping. Then you can remove froze and the timer. Even better would be to introduce a tolerance as the number of attempts increases - so they are allowed to overlap a little bit.

相关问答

更多
  • 试试这个 - 使用Math.Random和parseInt 在这里工作演示 $(document).ready(function(){ var r1 = (Math.random()*10); var r2 = (Math.random()*10); if(parseInt(r1)==parseInt(r2)) // IF BOTH ARE SAME { if(parseInt(r1)==10) { r2=r2-1; ...
  • 这是我将使用的代码: http://jsfiddle.net/26Pda/6/ var images = ['...']; //your images above var imagesused = []; $('.container div:not(.row)').each(function() { var rand = Math.Floor(Math.Random() * images.length); $(this).append('
    你的问题是你有一个奇怪的调用链接上下文点击$(this.hash) 。 在闭包之外更改为that = $(this)解决了问题。 $(function() { $('a[href*=#]:not([href=#])').click(function() { var that = $(this); $('html,body, .main').animate({ scrollTop: that.offset().top - 10 }, 1000); }); }); ...
  • 演示 因为你的div是动态生成的,请尝试: $(document).ready(function() { $(document).on('mouseover', '.destruct', function(){ $(this).css({background: '#000'}); }); }); 如果您使用的是旧版本的jquery,(> 1.7),请使用: $(".destruct").live("mouseover", function(){ $(this).css({b ...
  • 你有点模糊,所以这个答案很多。 //get height and width of view able area var height = $(window).height(); var width = $(window).width(); //change these to a random function of your choice. var randomx = 12; //rand(0, height) var randomy = 47; //rand(0, width) //subtract ...
  • 尝试以下方法: HTML
    CSS .wrapper { height:300px; position:relative; } .absPosition { position:absolute; ...
  • 我认为你在寻找的是: $(".small_div").css({'top': /*==>*/'+=5px'}); 要么 $(".small_div").css({'top': /*==>*/'-=5px'}); 但是你需要记住,你需要初始设置这些元素的初始top值,使用css或js ,否则+=或-=运算符将不会做任何事情。 检查更新的jsfiddle 。 (随着更改为-=和+=我添加top: 0px到.small_div规则) I think what you are looking for is: ...
  • 你需要指定position: absolute; 对于.img-X DIV,它们的位置将相对于最接近的定位祖先是绝对的。 那么你必须使用position: relative; .main以便它符合这个标准。 $(".img-1, .img-2, .img-3").each(function(index) { $(this).css({ left: Math.random() * ($(".main").width() - $(this).width()), top: Math.ra ...
  • 你有2个问题。 我看不到你在做什么,但A.link元素的大小只受视口高度的控制。 因此,如果视口高而窄,则链接很大,并且根本无法将它们全部拟合。 第二个问题是,如果无法放置链接,您的代码将永远尝试。 计时器永远不会触发,因为主脚本仍在忙着运行。 而不仅仅是做 while (!success) 最好做一些有限的尝试: var success = false; for (var attempt = 0; !success && attempt < 50; attempt++) { .... } 通 ...
  • 我不会为你编写代码,只是给你一个大纲: 为每个矩形的左上角和右下角创建坐标(将它们存储在数组或对象中),并将它们添加到坐标数组中。 在每个新矩形的生成中,检查其四个角的坐标中的任何一个是否大于任何其他矩形的左上角, 并且是否小于其他矩形的右下角(因此请检查是否有任何角落位于其中)任何其他矩形)。 如果不是,则为矩形生成新的随机坐标。 如果我不清楚你可以评论。 I'm not going to write the code for you, just give you an outline: Create c ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)