简体   繁体   English

3个jQuery脚本无法在HTML页面中一起使用

[英]3 jQuery Scripts Not working together in HTML page

I have an HTML code with 3 jQuery Scripts and they are not working together correctly can SomeOne Help me to solve this, please 我有一个包含3个jQuery脚本的HTML代码,但它们无法正常工作,可以有人帮我解决这个问题吗,请

I do to solve this problem as I don't have any experience in jQuery 我确实解决了这个问题,因为我没有jQuery的经验

and how can I fix this issue? 以及如何解决此问题?

It works now but one drop-down menu at a time how can I make them work together 现在可以使用,但是一次只能使用一个下拉菜单

  (function() { var options = $("#DropDownList2").html(); $('#DropDownList2 :not([value^="Fac"])').remove(); $('input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList2").html(options); $('#DropDownList2 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); (function() { var options = $("#DropDownList3").html(); $('#DropDownList3 :not([value^="Dip"])').remove(); $('input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList3").html(options); $('#DropDownList3 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); (function() { var options = $("#DropDownList4").html(); $('#DropDownList4 :not([value^="Dr"])').remove(); $('input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList4").html(options); $('#DropDownList4 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); 
 <script src = "https://code.jquery.com/jquery-latest.min.js"></script> Availabe Restaurants: <br> Burgers: <label><input type="radio" name="test" value="Garage" /> Burger Garage</label> <label><input type="radio" name="test" value="Burger" /> Hardee's</label> <label> <input type="radio" name="test" checked="checked" value="Factory" /> Burger Factory & More</label> <br> <select ID="DropDownList2" Height="18px" Width="187px"> <option Value="Factory_Style_1">Turbo Chicken</option> <option Value="Factory_Style_2">Twin Turbo Chicken</option> <option Value="Factory_Style_3">Garage Burger</option> <option Value="Burger_Style_1">Chicken Fille</option> <option Value="Burger_Style_2">Grilled Chicken Fillet</option> <option Value="Burger_Style_3">Jalapeno Chicken</option> <option Value="Garage_Style_1">Original Burger</option> <option Value="Garage_Style_2">Twin Turbo Chicken</option> <option Value="Garage_Style_3">Shuwa Burger</option> </select> <br> Desserts: <label><input type="radio" name="test1" checked="checked" value="Dip" /> Dip N Dip</label> <label><input type="radio" name="test1" value="Camel" /> Camel Cookies</label> <label> <input type="radio" name="test1" value="Ravenna" /> Ravenna Kunafa</label> <br> <select ID="DropDownList3" Height="18px" Width="187px"> <option Value="Dip_Style_1">Brownies Crepe</option> <option Value="Dip_Style_2">Fettuccine Crepe</option> <option Value="Dip_Style_3">Cream Puff Pyramid</option> <option Value="Camel_Style_1">Brownie Fondant</option> <option Value="Camel_Style_2">Lotus Pancake</option> <option Value="Camel_Style_3">Camel Lava</option> <option Value="Ravenna_Style_1">Konafa With Chocolate</option> <option Value="Ravenna_Style_2">Konafa Mabrooma With Cheese</option> <option Value="Ravenna_Style_3">Konafa Mabrooma With Cream</option> </select> <br> Beverages: <option>Dr.Shake</option> <option>Juice Lab</option> <option>Aseer Time</option> <label><input type="radio" name="test2" checked="checked" value="Dr" />Dr.Shake</label> <label><input type="radio" name="test2" value="Juice" /> Juice Lab</label> <label> <input type="radio" name="test2" value="Aseer" /> Aseer Time</label> <br> <select ID="DropDownList4" Height="18px" Width="187px"> <option Value="Dr_Style_1">Pressure Milkshake</option> <option Value="Dr_Style_2">Thermometer Milkshake</option> <option Value="Dr_Style_3">Brain Recovery Milkshake</option> <option Value="Juice_Style_1">G Lab</option> <option Value="Juice_Style_2">Summer Vimto</option> <option Value="Juice_Style_3">Summer Bubble Gum</option> <option Value="Aseer_Style_1">Hormone Happiness</option> <option Value="Aseer_Style_2">The King</option> <option Value="Aseer_Style_3">Berry Smothey</option> </select> 

Your issue here is that your radio button change events are overlapping each other so they don't have scope they just override each other, see the changes below where the radio input are now wrapped in a container and each event is scoped separately. 这里的问题是您的单选按钮更改事件彼此重叠,因此它们没有作用域,它们只能相互覆盖,请参见下面的更改,其中单选按钮输入现在包装在容器中,并且每个事件分别进行作用域。

it's worth noting that using scope around each section of dropdown and radio button set you could refactor your code into a single function can handle all events. 值得注意的是,在下拉菜单和单选按钮集的每个部分周围使用范围,可以将代码重构为单个函数,从而可以处理所有事件。

 (function() { var options = $("#DropDownList2").html(); $('#DropDownList2 :not([value^="Fac"])').remove(); $('.group1 input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList2").html(options); $('#DropDownList2 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); (function() { var options = $("#DropDownList3").html(); $('#DropDownList3 :not([value^="Dip"])').remove(); $('.group2 input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList3").html(options); $('#DropDownList3 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); (function() { var options = $("#DropDownList4").html(); $('#DropDownList4 :not([value^="Dr"])').remove(); $('.group3 input:radio').change(function(e) { var text = $(this).val(); $("#DropDownList4").html(options); $('#DropDownList4 :not([value^="' + text.substr(0, 3) + '"])').remove(); }); })(); 
 <script src = "https://code.jquery.com/jquery-latest.min.js"></script> Availabe Restaurants: <br> Burgers: <div class="group1"> <label><input type="radio" name="test" value="Garage" /> Burger Garage</label> <label><input type="radio" name="test" value="Burger" /> Hardee's</label> <label> <input type="radio" name="test" checked="checked" value="Factory" /> Burger Factory & More</label> </div> <br> <select ID="DropDownList2" Height="18px" Width="187px"> <option Value="Factory_Style_1">Turbo Chicken</option> <option Value="Factory_Style_2">Twin Turbo Chicken</option> <option Value="Factory_Style_3">Garage Burger</option> <option Value="Burger_Style_1">Chicken Fille</option> <option Value="Burger_Style_2">Grilled Chicken Fillet</option> <option Value="Burger_Style_3">Jalapeno Chicken</option> <option Value="Garage_Style_1">Original Burger</option> <option Value="Garage_Style_2">Twin Turbo Chicken</option> <option Value="Garage_Style_3">Shuwa Burger</option> </select> <br> Desserts: <div class="group2"> <label><input type="radio" name="test1" checked="checked" value="Dip" /> Dip N Dip</label> <label><input type="radio" name="test1" value="Camel" /> Camel Cookies</label> <label> <input type="radio" name="test1" value="Ravenna" /> Ravenna Kunafa</label> </div> <br> <select ID="DropDownList3" Height="18px" Width="187px"> <option Value="Dip_Style_1">Brownies Crepe</option> <option Value="Dip_Style_2">Fettuccine Crepe</option> <option Value="Dip_Style_3">Cream Puff Pyramid</option> <option Value="Camel_Style_1">Brownie Fondant</option> <option Value="Camel_Style_2">Lotus Pancake</option> <option Value="Camel_Style_3">Camel Lava</option> <option Value="Ravenna_Style_1">Konafa With Chocolate</option> <option Value="Ravenna_Style_2">Konafa Mabrooma With Cheese</option> <option Value="Ravenna_Style_3">Konafa Mabrooma With Cream</option> </select> <br> Beverages: <option>Dr.Shake</option> <option>Juice Lab</option> <option>Aseer Time</option> <div class="group3"> <label><input type="radio" name="test2" checked="checked" value="Dr" />Dr.Shake</label> <label><input type="radio" name="test2" value="Juice" /> Juice Lab</label> <label> <input type="radio" name="test2" value="Aseer" /> Aseer Time</label> </div> <br> <select ID="DropDownList4" Height="18px" Width="187px"> <option Value="Dr_Style_1">Pressure Milkshake</option> <option Value="Dr_Style_2">Thermometer Milkshake</option> <option Value="Dr_Style_3">Brain Recovery Milkshake</option> <option Value="Juice_Style_1">G Lab</option> <option Value="Juice_Style_2">Summer Vimto</option> <option Value="Juice_Style_3">Summer Bubble Gum</option> <option Value="Aseer_Style_1">Hormone Happiness</option> <option Value="Aseer_Style_2">The King</option> <option Value="Aseer_Style_3">Berry Smothey</option> </select> 

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

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