简体   繁体   English

jQuery-选择事件时

[英]jQuery - On Select Option Event

I am trying to make it so when somebody selects a certain option from my list it will trigger an event. 我试图做到这一点,所以当有人从我的列表中选择某个选项时,它将触发一个事件。 I know you are supposed to use the .change event but that isn't working for me. 我知道您应该使用.change事件,但这对我不起作用。

Checkout the code below. 签出下面的代码。

Select: 选择:

$co1 = array("English", "Math", "Science", "History");
for($c=1;$c<5;$c++){
    echo '<select name="c'.$c.'" id="course'.$c.'" class="selectpicker"><option selected="selected" id="course'.$c.'" value="course'.$c.'" name="'.$c.'">Course '.$c.'</option>';
    foreach ($co1 as &$cl1){
        echo '<option value="'.$cl1.'" name="'.$cl1.'">'.$cl1.'</option>';  
    }
    echo '</select>';
}

jQuery: jQuery的:

<script>
    $('#course1').change(function(){
        $("#course2 option[value='Math']").each(function() {
            $(this).remove();
        });
    })
</script>

I basically used PHP to make it so there are four different select options with the same options but different values for a grading system. 我基本上是用PHP来制作的,所以有四个不同的选择选项,它们具有相同的选项,但评分系统的值却不同。 I want to use this script to make it so when you select Math for the first course it will remove it from the list in the other 3 courses. 我想使用此脚本来制作它,因此当您为第一门课程选择“数学”时,它将从其他3门课程的列表中将其删除。

Any ideas how this would work? 任何想法这将如何工作? I'm up for suggestions on other ways to do this. 我正在寻求其他方法的建议。

Use delegation 使用delegation

$('body').delegate('#course1','change',function(){
    $("#course2 option[value='Math']").each(function() {
        $(this).remove();
    });
})

Try: 尝试:

$("body").on("change","#course1",function () {
     $("#course2").find("option[value='Math']").remove();
});

Fiddle here. 在这里摆弄。

Attribute name and id just for only select tag. 仅用于select标记的属性nameid Open this link to see standard select option html tag. 打开此链接以查看标准选择选项html标记。 If you do that, you have two same id there. 如果这样做,则那里有两个相同的ID。 <select> , and your first <option> . <select>和您的第一个<option>

try to change your PHP become : 尝试将您的PHP更改为:

$co1 = array("English", "Math", "Science", "History");
for($c=1;$c<5;$c++){
    echo '<select name="c'.$c.'" id="course'.$c.'" class="selectpicker">
             <option selected="selected" value="course'.$c.'">Course '.$c.'</option>';
             foreach ($co1 as &$cl1){
                echo '<option value="'.$cl1.'">'.$cl1.'</option>';  
             }
    echo '</select>';
}

This is the shortest I could come up with: 这是我能想到的最短的时间:

$('#course1').change(function () {
    $('#course2').find('option:gt(0)').remove().end().append($(this).find('option:gt(0)[value!=' + $(this).val() + ']').clone())
})

jsFiddle example jsFiddle示例

this will hide the option from course2 if same is selected as course1 and show if user selects another one in course1 如果选择了课程1,则将从课程2中隐藏该选项,并显示用户是否在课程1中选择了另一个

$('#course1').change(function () {
  var val = $(this).val()

  if($('#course2').val()==val)                
    $('#course2').val($('#course2').children('option:first').val())

  $("#course2 option").each(function () {

   if ($(this).val() == val) 
     $(this).hide();  //change this line to $(this).remove() to remove permanently  
                     //and remove the else part below
   else 
     $(this).show()

 });
 })

DEMO 演示

Note that after removing completely you will not be able to show it again hence i will recommend using show and hide on change of course1 请注意 ,完全删除后,您将无法再次显示它,因此我建议您在更改course1时使用show and hide

I hope I got right what you want to do. 希望我做对了。 Try this fiddle 试试这个小提琴

You could just .hide() the option which matches the current selected and to show the before hidden again just .show() the current :hidden elements? 您可以仅.hide()匹配当前所选内容的选项,然后再次显示.show()显示当前:hidden元素?

$('#course1').change(function(){
    var sel = $(this).val(); 
    $("#course2 option:hidden").show();
    $("#course2 option[value='"+sel+"']").each(function() {
        $(this).hide();
    });    
});

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

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