简体   繁体   English

jQuery hide()/ show()可在FF / IE上运行,但不能在Chrome上运行

[英]JQuery hide()/show() working on FF/IE but not Chrome

I have a web page in which I use sql/php to create a select where options are in my database : the value of each option is the id on the database. 我有一个网页,在其中我使用sql / php在数据库中的选项中创建一个select:每个选项的值是数据库中的id。 With this, I use JQuery 3.3.1 : this is the first time I'm using it so I think my syntax might be bad. 这样,我使用JQuery 3.3.1:这是我第一次使用它,因此我认为我的语法可能不好。

First of all there is 2 type of datas : about price and about time. 首先,有两种类型的数据:关于价格和关于时间。 I made 2 radio button and when the "price" button is checked, the price list is shown while the time list is hidden. 我做了2个单选按钮,当选中“价格”按钮时,价格列表显示,而时间列表隐藏。

This part of the code works fine on FF/IE/Chrome. 这部分代码可在FF / IE / Chrome上正常运行。

Now I want to display more informations about the option selected so I made a div for every option with style='display:none' and when the option is selected I show the div where the id of the div = the value of the option. 现在,我想显示有关所选选项的更多信息,因此我为每个带有style ='display:none'的选项制作了一个div,当选择了该选项时,我将显示div,其中div的ID =选项的值。

This part of the code works fine on FF/IE but not Chrome. 这部分代码在FF / IE上可以正常运行,但在Chrome上则不能。

Here's my code, I replaced database calls by just writing few examples of what I can get here : 这是我的代码,我仅通过写一些我在这里可以得到的例子来替换数据库调用:

 $(document).ready(function() { $('input[type="radio"]').click(function() { if ($(this).attr("value") == "prix") { $("#prix").show('fast'); $("#temps").hide('fast'); $("#" + $(this).attr("value") + "_prix").siblings().hide('fast'); $("#" + $(this).attr("value") + "_prix").show('fast'); } if ($(this).attr("value") == "temps") { $("#prix").hide('fast'); $("#temps").show('fast'); $("#" + $(this).attr("value") + "_temps").siblings().hide('fast'); $("#" + $(this).attr("value") + "_temps").show('fast'); } }); $('input[type="radio"]').trigger('click'); }); function showDetailsCriterePrix(id) { $("#" + id + "_prix").siblings().hide('fast'); $("#" + id + "_prix").show('fast'); } function showDetailsCritereTemps(id) { $("#" + id + "_temps").siblings().hide('fast'); $("#" + id + "_temps").show('fast'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <br><br> Le critère à supprimer concerne le : <input type="radio" name="cat_critere" value="temps" required>Temps <input type="radio" name="cat_critere" value="prix" checked required>Prix <div id="prix" style="display:none"> <br><br> Choisissez un critère de prix à supprimer : <select name="idToDelPrix"> <option value="1" onclick='showDetailsCriterePrix(this.value);'>Nombre de caisses [AVE] </option> <option value="2" onclick='showDetailsCriterePrix(this.value);'>Nombre d'associés [AVE] </option> <option value="3" onclick='showDetailsCriterePrix(this.value);'>Nombre de salariés [AVE] </option> <option value="4" onclick='showDetailsCriterePrix(this.value);'>Nombre de TVA [AVE] </option> </select> <div id="wrapperPrix"> <div id="1_prix" style="display:none"> Taux prix : 175<br> Taux abattement prix : 0<br> Champ obligatoire : Oui</div> <div id="2_prix" style="display:none"> Taux prix : 100<br> Taux abattement prix : 0<br> Champ obligatoire : Oui</div> <div id="3_prix" style="display:none"> Taux prix : 100<br> Taux abattement prix : 0<br> Champ obligatoire : Oui</div> <div id="4_prix" style="display:none"> Taux prix : 1<br> Taux abattement prix : 0<br> Champ obligatoire : Oui</div> </div> </div> <div id="temps" style="display:none"> <br><br> Choisissez un critère de temps à supprimer : <select name="idToDelTemps"> <option value="1" onclick='showDetailsCritereTemps(this.value);'>Temps1 [AVE] </option> <option value="2" onclick='showDetailsCritereTemps(this.value);'>Temps2 [AVE] </option> <option value="3" onclick='showDetailsCritereTemps(this.value);'>Temps3 [AVE] </option> <option value="4" onclick='showDetailsCritereTemps(this.value);'>Temps4 [AVE] </option> </select> <div id="wrapperTemps"> <div id="1_temps" style="display:none"> Taux temps : 175<br> Taux abattement temps : 0<br> Champ obligatoire : Oui</div> <div id="2_temps" style="display:none"> Taux temps : 100<br> Taux abattement temps : 0<br> Champ obligatoire : Oui</div> <div id="3_temps" style="display:none"> Taux temps : 100<br> Taux abattement temps : 0<br> Champ obligatoire : Oui</div> <div id="4_temps" style="display:none"> Taux temps : 1<br> Taux abattement temps : 0<br> Champ obligatoire : Oui</div> </div> </div> 

I don't know why this is not working on Chrome, I tried to replace the .click by .change, I tried with and without parameters in hide()/show(). 我不知道为什么这在Chrome上不起作用,我尝试将.click替换为.change,尝试在hide()/ show()中使用和不使用参数。

If you guys have any idea it would be very kind ! 如果你们有任何想法,那就太好了!

(Sorry, my code is in French but I think it's understandable even if you don't know French!) (对不起,我的代码是法语,但即使您不懂法语,我也认为这是可以理解的!)

EDIT : Answered by reporter in the comments, thank you very much ! 编辑记者在评论中回答,非常感谢!

Instead of using show()/hide() we can use toggleClass(). 代替使用show()/ hide(),我们可以使用toggleClass()。 In this case I had to : 在这种情况下,我必须:

1- Create a css class with "display:none;" 1-使用“ display:none;”创建一个css类

2- Replace the style of my divs with this class 2-用该类替换我的div样式

[2.5- Add an id to my selects (should have done this before) ] [2.5-将ID添加到我的选择中(之前应该这样做)]

3- Add an event on my selects that get the value of the selected option and toggle the class of the div with the same id. 3-在我的选择上添加一个事件,该事件获取所选选项的值,并使用相同的ID切换div的类。

4- Remove the now useless showDetailsCriteres functions 4-删除现在无用的showDetailsCriteres函数

You can simply bind onchange event of dropdown rather binding onclick event to each option. 您可以简单地将下拉列表的onchange事件绑定,而不是将onclick事件绑定到每个选项。

     function PrixChange(ctrl){
     var id = $(ctrl).val();
       $("#" + id + "_prix").siblings().hide('fast');
        $("#" + id + "_prix").show('fast');

    }

 <select name="idToDelPrix" onchange="PrixChange(this);">
      <option value="1" onclick='showDetailsCriterePrix(this.value);'>Nombre de caisses [AVE] </option>
      <option value="2" onclick='showDetailsCriterePrix(this.value);'>Nombre d'associés [AVE] </option>
      <option value="3" onclick='showDetailsCriterePrix(this.value);'>Nombre de salariés [AVE] </option>
      <option value="4" onclick='showDetailsCriterePrix(this.value);'>Nombre de TVA [AVE] </option>
   </select>

You can toggle class name defining property none. 您可以切换没有定义属性的类名。 You can try .on event instead of .click event 你可以试试.on事件代替.click事件

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

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