如何在jquery

时间:2018-01-24 16:55:59

标签: javascript jquery

你能帮我解决这个问题吗? 问题是这个。

我有多个具有相同类名但不同ID的选择列表。如果用户选择已在其他选择列表中选择的相同选项,则应该有警报。并且在警告之后,单击或更改的当前选择列表应恢复为先前的值。例如,我们有3个选择框名称为A1,A2,A3。每个都具有相同的值,例如1,2,3,4。 A1选择值为1,A2选择值为2,A3选择值为3.现在,如果您想将A1值从1更改为2,则应该有一个类似"已经选择"的警告。此警报后,A1列表应恢复为原始值1.我尝试了以下代码。在此代码中,我可以获得已存在的警报,但最后一次更改选择框的值将更改为新的。



    
    $(document).ready(function(){
       var init = true;
       
       if(init == true){
       		var a = new Array();
       		$(".brand-list").each(function(i, obj){
          	var current_obj_id = $(this).attr('id');
          	var current_obj_value = $('#'+ current_obj_id + " option:selected").val();
          	a[current_obj_id] = current_obj_value;
       		});
          init = false;
       }
       
       
       $(".brand-list").change(function(){
       	a[$(this).attr('id')] = $('#'+ $(this).attr('id') + " option:selected").val();
        
        var current_selected_obj_id = $(this).attr('id');
        var current_selected_obj_value = $('#'+ $(this).attr('id') + " option:selected").val();   
        
        $(".brand-list").each(function(i, obj){
          var current_obj_id = $(this).attr('id');
          var current_obj_value = $('#'+ current_obj_id + " option:selected").val();
          if(current_obj_id != current_selected_obj_id){
             if(current_selected_obj_value == current_obj_value){
               
                alert("current element global value: "+ a[current_selected_obj_id]);
             
             		$('#'+ current_selected_obj_id + " option[value=" + a[current_selected_obj_id] +"]").attr('selected','selected')
             
               alert("already selected");
             }
          }else{
             a[current_obj_id] = current_obj_value;
          }
          
       });
        
       });
       
       
       $( ".brand-list" ).focus(function() {
       
       		var current_selected_obj_id = $(this).attr('id');
        	var current_selected_obj_value = $('#'+ $(this).attr('id') + " option:selected").val();   
      		a[current_selected_obj_id] = current_selected_obj_value;
          console.log(a[current_selected_obj_id]);
    		}); 
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id="inputBrand-1" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>
    
    <select id="inputBrand-2" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>
    
    <select id="inputBrand-3" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3" selected="selected">3</option>
    <option value="4">4</option>
    </select>
&#13;
&#13;
&#13;

希望你能理解我的问题,因为我不能问问社区成员可以轻易理解的问题。感谢

3 个答案:

答案 0 :(得分:1)

不要通过让用户选择某些东西来惹恼用户,而是告诉用户他们可以通过禁用以前的选择或完全删除它们来获得更好的用户体验。

这是一种管理过滤/删除其他选择的方法

&#13;
&#13;
var $selects = $(".brand-list"),
  // store a cloned set of all options 
  $storedOptions = $selects.first().children().clone().removeAttr('selected'),
  // whether to always include "SELECT A BRAND"
  alwaysShowDefault = true; 

$selects.change(function() {

  // create array of all the selected values
  var allValues = $selects.map(function() {
    return $(this).val()
  }).get();
  
  // loop through each select to create filtered options
  $selects.each(function(i) {
    // new set of cloned and filtered options for this select instance
    var $opts =  $storedOptions.clone().filter(function() {     
      if(+this.value === -1){
         return alwaysShowDefault || +allValues[i] === -1;
      }    
      return allValues[i] === this.value || allValues.indexOf(this.value) === -1;
    });
    // update filtered options and reset current value
    $(this).html($opts).val(allValues[i])
  })


// trigger one change on page load to filter what is already selected
}).first().change();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id="inputBrand-1" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>

<select id="inputBrand-2" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>

<select id="inputBrand-3" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3" >3</option>
    <option value="4">4</option>
    </select>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以保存以前的值并进行检查

&#13;
&#13;
    $(document).ready(function(){
       var init = true;
       
       if(init == true){
       		var a = new Array();
       		$(".brand-list").each(function(i, obj){
          	var current_obj_id = $(this).attr('id');
          	var current_obj_value = $('#'+ current_obj_id + " option:selected").val();
          	a[current_obj_id] = current_obj_value;
       		});
          init = false;
       }
	   
	   function hasConflict(input){
		   var conflict = false;
		   $(".brand-list").not(input).each(function(i, obj){
			   if($(this).val()==input.val()){
				   conflict = true;
				   return false; //break the loop
			   }
		   });
		   
		   return conflict;
	   }
       
       
       $(".brand-list").change(function(){
		   var $this = $(this); //recycle object
		   if(hasConflict($this)){
		       $this.val($this.data('prev'));
			   alert("Conflict"); //do whatever
		   }            
       });
       
       
       $( ".brand-list" ).on('click focus',function() {
           $(this).data('prev',$(this).val());
    	}); 
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select id="inputBrand-1" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>
    
    <select id="inputBrand-2" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    
    </select>
    
    <select id="inputBrand-3" class="brand-list">
    <option value="-1">SELECT A BRAND</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3" selected="selected">3</option>
    <option value="4">4</option>
    </select>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

实际上,您目前正在做的是在每个选项元素上手动设置所选属性。这不是一个好方法,因为它不会删除相同select元素的选项的其他属性。因此,请在源代码中使用以下代码。

$(e.currentTarget).val(-1);

用以上代码替换以下代码

$('#'+ current_selected_obj_id + " option[value=" + a[current_selected_obj_id] +"]").attr('selected','selected')