简体   繁体   English

Javascript .change()错误。 不会自动更改Chrome中的选择框

[英]Javascript .change() error. not automatically changing select box in chrome

Hello I have an input box, when I enter a value in and based on the value changes my select box. 您好,我有一个输入框,当我在其中输入值并根据该值更改选择框时。 I had this working for a couple weeks now and all of a sudden its not working.. This is the most important piece of my app so big problem..works in firefox not chrome. 我有这个工作了几个星期,突然间它不起作用..这是我的应用程序中最重要的部分,所以很大的问题..在Firefox中无法使用chrome。

Now iv come down to the source of the problem but not quite sure how to fix it. 现在,iv可以解决问题的根源,但不确定如何解决。 This works in firefox but not in chrome. 这适用于Firefox,但不适用于Chrome。 $('#agreement_selected_service option:visible:first').prop('selected', true).change(); <-- this is what i used to make the box change automatically every time an input/keyup/change was made to word-count. <-这就是我用来每次对单词计数进行输入/键入/更改时自动更改框的功能。 I added a log console.log('test'); 我添加了一个日志console.log('test'); to see if my script made it to where the .change() occurs and the console prints out.... so this $('#agreement_selected_service option:visible:first').prop('selected', true).change(); 看看我的脚本是否到达了.change()发生的位置并且控制台输出了...。所以,这个$('#agreement_selected_service option:visible:first').prop('selected', true).change(); seems to have something wrong now?? 现在好像出了点问题?? thanks for the help. 谢谢您的帮助。

All in all my word-count input on any keyup isn't automatically changing the select option to the next first available. 总而言之,我在任何按键上输入word-count并不会自动将选择选项更改为下一个可用的选项。 only is working in firefox. 仅在Firefox中工作。 The select box "first option" / "currently selected" just stays the same even when its disabled. 即使禁用,选择框“第一个选项” /“当前选择的”也保持不变。 It makes me have to manually change the select box to change the service. 这使我不得不手动更改选择框才能更改服务。 but should be the first option that currently isnt disabled. 但应该是当前未禁用的第一个选项。

input box id = #word-count and select box id = #agreement_selected_service 输入框ID = #word-count ,选择框ID = #agreement_selected_service

 <div class="col-xs-7 "> <div class="form-group" style=""> <label>Plan</label> <%= f.select :selected_service, ServiceType.all.order("id asc").collect {|x| [x.name, x.word_count]}, {}, :style => "width:100%;"%> </div> </div> <div class="col-xs-5"> <div class="form-group" style=""> <label>Word Count</label> <%= f.text_field :char_amount, class: "form-control", id: 'word-count', style: 'width:120px;', placeholder: "Ex. 500" %> </div> </div> 

price_calculation.js price_calculation.js

 $(function() { $("#word-count").bind("change keyup input", function() { var word_count = $('#word-count').val(); var select = $('#agreement_selected_service'); var options = $('#agreement_selected_service option'); if (word_count < 7000) { options.each(function() { var tr = $(this); if (word_count <= 650 && tr.val() < 22000) { tr.show(); tr.prop("disabled", false); } else if (word_count > 650 && tr.val() <= 650) { tr.hide(); tr.prop("disabled", true); } else if (word_count >= 4000 && tr.val() <= 24999) { tr.show(); tr.prop("disabled", false); } else if (tr.val() > 22000) { tr.hide(); tr.prop("disabled", true); } else { tr.show(); tr.prop("disabled", false); } }); console.log("test"); $('#agreement_selected_service option:visible:first').prop('selected', true).change(); } else if (word_count >= 7000 && word_count <= 13999) { options.each(function() { var tr = $(this); if (tr.val() < 7000 || tr.val() >= 30000) { tr.hide(); tr.prop("disabled", true); } else if (word_count >= 7000 && tr.val() <= 30000) { tr.show(); tr.prop("disabled", false); } else { tr.show(); tr.prop("disabled", false); } }); $('#agreement_selected_service option:visible:first').prop('selected', true).change(); } else if (word_count >= 14000 && word_count < 22000) { options.each(function() { var tr = $(this); if (tr.val() < 14000 || tr.val() >= 30000) { tr.hide(); tr.prop("disabled", true); } else if (word_count < 22000 && (tr.val() >= 14000 && tr.val() < 22000)) { tr.show(); tr.prop("disabled", false); } else { tr.show(); tr.prop("disabled", false); } }); $('#agreement_selected_service option:visible:first').prop('selected', true).change(); } else if (word_count >= 22000 && word_count < 30000) { options.each(function() { var tr = $(this); if (tr.val() < 22000 || tr.val() >= 30000) { tr.hide(); tr.prop("disabled", true); } else if (word_count < 25000 && tr.val() >= 24999) { tr.show(); tr.prop("disabled", false); } else if (word_count >= 25000 && word_count < 30000 && tr.val() < 25000) { tr.hide(); tr.prop("disabled", true); } else { tr.show(); tr.prop("disabled", false); } }); $('#agreement_selected_service option:visible:first').prop('selected', true).change(); } else if (word_count >= 30000) { options.each(function() { var tr = $(this); if (tr.val() == 30000) { tr.show(); tr.prop("disabled", false); } else { tr.hide(); tr.prop("disabled", true); } }); $('#agreement_selected_service option:visible:first').prop('selected', true).change(); } }); $("#agreement_selected_service").change(function() { console.log("changed"); }); }); 

I do not think the option:visible:first is doing what you think it does. 我不认为option:visible:first在做您认为做的事。

According to the jQuery documentation of :visible 根据:visible的jQuery文档

All option elements are considered hidden, regardless of their selected state. 不论所有option元素处于selected状态,都将被视为隐藏。

So you might want to change that to 因此,您可能需要将其更改为

$('#agreement_selected_service option').filter(function(){
    return !this.disabled;
}).first().prop('selected', true).change();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM