简体   繁体   English

JavaScript:根据另一个下拉列表从下拉列表中删除不同的选项标签?

[英]Javascript: Remove different option-tags from a dropdown list depending on another dropdown?

Yeah, the title is a mess. 是的,标题很乱。

Here's what i'm trying to do: I have one main dropdown list where i can choose between, let's say, two options. 这是我要执行的操作:我有一个主要的下拉列表,可以在其中选择两个选项。 Option1, and Option2. 选项1和选项2。 If Option1 is selected, i don't want anything to happen. 如果选择Option1,我不希望发生任何事情。 If Option2 is selected, i want to remove one option-tag from another dropdown list. 如果选择了Option2,我想从另一个下拉列表中删除一个选项标签。

<select name="main_dropdown">
    <option value="Option1">Option 1</option>
    <option value="Option2">Option 2</option>
</select>

<select name="secondary_dropdown">
    <option value="Option1">Option 1</option>
    <option value="Option2">Option 2</option> // Let's say i want to remove this option tag if Option2 in main_dropdown is selected.
    <option value="Option2">Option 2</option>
</select>

Thanks! 谢谢!

I think you'll get better responses and more attention if your questions include code demonstrating your own attempts to find an answer, rather than asking others to write the code that you need. 我认为,如果您的问题包括演示自己寻找答案的代码,而不是要求其他人编写所需的代码,则您会得到更好的答复和更多关注。 Not just because people don't want to feel like their doing free work, but it also becomes much more difficult to answer. 不仅因为人们不想自己做自由工作,而且回答起来也变得更加困难。

For example, even though what you're trying to do is pretty basic javascript, there are dozens of different ways you could solve it. 例如,即使您尝试做的是非常基本的javascript,也可以通过许多不同的方法来解决它。 Here's a quick working answer for your question (using jQuery): 这是您的问题的快速有效答案(使用jQuery):

<script>
  $(document).ready(function(){
    $('select[name=main_dropdown]').bind('change',function(){
      if($(this).val() == 'Option2') $('select[name=secondary_dropdown] option[value=Option2]').remove();
    });   
  });
</script>

<select name="main_dropdown">
    <option value="Option1">Option 1</option>
    <option value="Option2">Option 2</option>
</select>

<select name="secondary_dropdown">
    <option value="Option1">Option 1</option>
    <option value="Option2">Option 2</option> // Let's say i want to remove this option tag if Option2 in main_dropdown is selected.
    <option value="Option2">Option 2</option>
</select>

See it in action. 看到它在行动。

That said, this solution is incredibly brittle. 也就是说,该解决方案非常脆弱。 It'll do what you want in this case , but isn't a very good solution if your relationships change, or code gets more complex, etc. Without example code, it's impossible to tell what criteria you're using to target the options and remove them, so it's impossible to do you much good. 在这种情况下 ,它会做您想要的事情 ,但是如果您的关系发生变化,或者代码变得更加复杂等,这不是一个很好的解决方案。没有示例代码,就无法确定您要使用哪些标准来定位选项并删除它们,因此不可能为您带来很多好处。

I recommend starting with the jQuery library, which has some great Tutorials . 我建议从jQuery库开始,该库具有一些很棒的Tutorials You should be able to solve problems like this on your own pretty quickly if you give it a shot. 如果您试一试,您应该能够很快自己解决类似的问题。 If your code ends up not working and you don't know where to turn, fear not -- I'm sure people here will be happy to help you out. 如果您的代码最终无法正常工作,并且您不知道该去哪里,请不要担心-我相信这里的人会很乐意为您提供帮助。

<option value="Option2" id="ToBeOrNotToBe">Option 2</option>

If the value of 'main_dropdown' is "Option2" (do a comparison), remove document.getElementById("ToBeOrNotToBe") from "secondary_dropdown" using .removeChild() . 如果'main_dropdown'的值为“ Option2”(进行比较),则使用.removeChild()从“ secondary_dropdown”中删除document.getElementById(“ ToBeOrNotToBe”)。 I would suggest giving all your selects a unique id as well as name so you can use document.getElementById() to get your element, then manipulate everything using .removeChild(). 我建议给所有选择的对象一个唯一的ID和名称,以便您可以使用document.getElementById()获取元素,然后使用.removeChild()操作所有内容。 The parameter you would pass to removeChild() would be your option2. 您要传递给removeChild()的参数将是您的option2。

However, removeChild() returns the object it deletes, so store it into another variable, so if the user changes his mind with the main drop down, appendChild() the removed option2 back as a child to "secondary_dropdown" . 但是,removeChild()返回它删除的对象,因此将其存储到另一个变量中,因此,如果用户改变主意,则将已删除的option2作为子对象添加到“ secondary_dropdown”作为子项。

Also, in my opinion, first construct the main dropdown, and once they have selected a value, then construct the second dropdown using appendChild() into the document itself. 另外,我认为,首先构造主下拉列表,一旦他们选择了一个值,然后使用appendChild()将第二个下拉列表构造到文档本身中。 If you look, http://www.w3schools.com/tags/tag_option.asp shows the event listeners you can attach to options, so once an option is selected, insert the second dropdown below the first one, and construct it accordingly. 如果您看过, http://www.w3schools.com/tags/tag_option.asp会显示您可以附加到选项的事件侦听器,因此一旦选择了选项,请在第一个下拉菜单下方插入第二个下拉菜单,并进行相应的构造。

If you just want a fast and simple solution, use an event listener to activate when an option in the main dropdown is selected, then remove the option2 or append it, accordingly. 如果只想提供快速简单的解决方案,请在选择主下拉菜单中的选项时使用事件侦听器激活,然后相应地删除option2或将其附加。

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

相关问题 JavaScript:动态下拉菜单用于另一个隐藏的下拉菜单和其他选项 - JavaScript : Dynamic Dropdown for Another Hidden Dropdown and Different Option 如何在javascript中的另一个下拉列表中填充排除任何一个选项的下拉列表? - How can I populate a dropdown list excluding any one option from another dropdown list in javascript? 从下拉列表中删除选择的选项 - Remove selected option from dropdown list 从下拉列表中将选定的目标删除到另一个下拉列表 - Remove selected destination from dropdown list to another dropdown list 如何根据第三个下拉列表项的选择切换不同的标签(下拉列表和列表)? - how to switch with different tags ( dropdown and list ) depending on the selection of third dropdown's item? jQuery-根据下拉列表中的选项更改复选框值 - Jquery - changing checkbox value depending on option from Dropdown list 使用JavaScript从下拉列表中获取选项标题 - Get option title from dropdown list with javascript Js,根据先前的下拉选项选择输出选项下拉列表 - Js, output a option dropdown list depending on previous dropdown option choice 根据下拉列表中的选定选项显示数据库中的数据 - Show data from database depending on selected option in a dropdown list 根据选择下拉列表中的单击选项更改标记 - Change a marker depending on the clicked option from a select dropdown list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM