简体   繁体   English

使用Javascript阻止HTML下拉框中的某些选项

[英]Block certain options in a HTML drop down box using Javascript

I know this question or similar has been asked a few times however I can't find a suitable answer. 我知道这个问题或类似问题已经被问过几次了,但是我找不到合适的答案。 I am creating a website where people can book bus journeys. 我正在创建一个网站,人们可以在该网站上预订公共汽车旅行。 I have two drop down boxes, both with the same locations. 我有两个下拉框,都位于相同的位置。 I want it so when a certain departure is selected, only certain locations within the destination drop down box are enabled. 我想要这样,当选择某个出发点时,仅启用目标下拉列表框中的某些位置。 For example, if London is selected in the first drop down, you cannot select London or any of the locations in which the bus does not travel to in the second drop down. 例如,如果在第一个下拉菜单中选择了伦敦,则不能在第二个下拉菜单中选择伦敦或公交车未行驶到的任何位置。

Here is our HTML 这是我们的HTML

     <b> Departure: </b> &nbsp;
     <select name="leave" id="leave" required>
     <option value="" selected id="disabled">-- Select --</option>
     <option value="Bristol" id="Lbristol"> Bristol </option>
     <option value="Newcastle" id="Lnewcastle"> Newcastle </option>
     <option value="Manchester" id="Lmanchester"> Manchester </option>
     <option value="London" id="Llondon"> London </option>
     <option value="Glasgow" id="Lglasgow"> Glasgow </option>
     </select>

    <b> Destination: </b> &nbsp;
    <select name="arrive" id="arrive" required>
    <option value="" selected id="disabled">-- Select --</option>
    <option value="Bristol" id="Abristol"> Bristol </option>
    <option value="Newcastle" id="Anewcastle"> Newcastle </option>
    <option value="Manchester" id="Amanchester"> Manchester </option>
    <option value="London" id="Alondon"> London </option>
    <option value="Glasgow" id="Aglasgow"> Glasgow </option> 
    </select>

We have tried multiple different ideas of JavaScript but can only get it to block out one or all options in the second drop down box. 我们已经尝试了多种不同的JavaScript思想,但是只能阻止第二个下拉框中的一个或所有选项。

Here is one of the long ideas of javascript click here Jfiddle I stored it in Jfiddle but don't actually know how to work it so it doesn't run sorry for being pretty useless 这是javascript的长创意之一,请单击此处。Jfiddle我将其存储在Jfiddle中,但实际上并不知道如何工作,因此它不会因为毫无用处而感到抱歉

Thanks in advance. 提前致谢。

https://jsfiddle.net/bt4v846c/1/ https://jsfiddle.net/bt4v846c/1/

$("#Abristol").prop('disabled', true);
$("#Anewcastle").prop('disabled', true);

As a sidenote, disabled is it's own property and not put in the ID field. 附带说明,disabled是其自身的属性,不放在ID字段中。

Relevant question: Disable Drop Down Option using jQuery 相关问题: 使用jQuery禁用下拉选项

You could remove the option from the dropdown based on a variable in my example its value. 您可以在示例中根据变量的值从下拉列表中删除该选项。

 $(document).ready(function(){
    var value = "London";
    $("#arrive option[value='"+value+"']").remove();
  });

Here is an interesting solution using my favorite, and often underutilized, conditional statement switch . 这是一个有趣的解决方案,它使用了我最喜欢的并且经常未得到充分利用的条件语句switch You are going to want to disable destination dynamically based on the selection in the departure select element, hence the use of an on change handler and simple switch statement to delegate who should be disabled. 你将要动态地根据在出发选择禁用目的地 select元素,因此对使用的change处理和简单的switch语句来委派应该被禁用。

JQuery jQuery查询

$('#leave').on('change', function () {
$('#arrive').children('option').prop('disabled', false);

switch ($(this).val()) {

    case "Bristol":
        $("#Abristol").prop('disabled', true);
        break;

    case "Newcastle":
        $("#Anewcastle").prop('disabled', true);
        break;

    case "Manchester":
        $("#Amanchester").prop('disabled', true);
        break;

    case "London":
        $("#Alondon").prop('disabled', true);
        break;

    case "Glasgow":
        $("#Aglasgow").prop('disabled', true);
        break;
   }
});

Check out the working: JSFiddle 查看工作原理JSFiddle

NOTE: a larger, or rather a lengthier select element may call for a for loop for both robusticity and concision. 注意:一个更大或更长的select元素可能会要求for循环以提高鲁棒性和简洁性。

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

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