如何保留所有选择元素在jQuery中选择的值 [英] how to preserve all select elements selected values in jquery

查看:55
本文介绍了如何保留所有选择元素在jQuery中选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能帮我解决这个问题吗? 问题是这样.

Can you help me to solve this problem. The problem is this.

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

I have multiple select lists with same class name but different ids. If user select same option which is already selected in other select list then there should be an alert. and after alert the current select list which is clicked or changed should be restore to the previous value. for example we have 3 select boxes name as A1, A2, A3. Each one have same values for example 1,2,3,4. A1 selected value is 1, A2 selected value is 2 and A3 selected value is 3. Now if your want to change A1 value from 1 to 2 then there should be an alert like "Already selected". After this alert A1 list should be restored back to its original value which is 1. I tried the following code. In this code I can get already existing alert but the value of the last changes select box is changed to new one.

    
    $(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>

希望您能理解我的问题,因为我没有要问哪个社区成员可以轻松理解的问题.谢谢

Hopefully you can understand my question, because I'm not get to ask question which community members can understand easily. Thanks

推荐答案

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

You can save the previous value and check it then

    $(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());
    	}); 
    });

<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>

这篇关于如何保留所有选择元素在jQuery中选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆