数组效果不佳

时间:2018-06-08 02:22:33

标签: jquery ajax

选择复选框时,我的阵列上没有值。如果您选择多个复选框,有时会存储值。主要是存储在数组上的最后一个复选框值。这样做的目的是过滤来自国家和类别的数据,因此我需要提取国家和类别复选框值并将其作为帖子提交到php文件。



var selectedFilter = new Array();
        var selectedFilterCat = new Array();
        $('.filter-check').change(function(){
        	
        	$('.country').each(function(){
        		if($(this).is(':checked')){
        			selectedFilter.push(  $(this).val() )	
        			/*console.log(selectedFilter);*/
        		}else{
        			var index = selectedFilter.indexOf( $(this).val() )
        			/*console.log(index)*/
        			selectedFilter.splice(index, 1 )
        		}
        	})
        		$('.category').each(function(){
        		if($(this).is(':checked')){
        			selectedFilterCat.push(  $(this).val() )	
        		}else{
        			var indexcat = selectedFilterCat.indexOf( $(this).val() )
        			/*console.log(index)*/
        			selectedFilterCat.splice(indexcat, 1 )
        		}
        	})
        	
        	
        
        console.log(selectedFilter);
        console.log(selectedFilterCat);
        	
        	//Ajax	
        	var a =	JSON.stringify(selectedFilter);
        	var b =	JSON.parse(a);
        	var c =	JSON.stringify(selectedFilterCat);
        	var d =	JSON.parse(c);
        	$.ajax({
        	  method: "GET",
        	  url: "/wjo/call-job-list.php",
        	  data: { filter: b },
        	  success: function(result){
                $("#joblistings").html(result);
            }
        	})
        	
        })

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navcontainer">
    <ul>
    <li>
    <label class=" container-check">Australia
    	<input class="filter-check country" type="checkbox" name="country" value="Australia">
    </label>
    </li>
    
    <li>
    <label class=" container-check">United Kingdom
    	<input class="filter-check country" type="checkbox" name="country" value="United Kingdom">
    </label>
    </li>
    
    <li>
    <label class=" container-check">USA
    	<input class="filter-check country" type="checkbox" name="country" value="USA">
    </label>
    </li>
    
    <li>
    <label class=" container-check">New Zealand
    	<input class="filter-check country" type="checkbox" name="country" value="New Zealand">
    </label>
    </li>
    </ul>
    </div>
    <div id="navcontainer1">
    <ul>
    <li>
    <label class=" container-check">Vineyard / Viticulture
    	<input class="filter-check category" type="checkbox" name="country" value="Vineyard / Viticulture">
    </label>
    </li><li>
    <label class=" container-check">Winemaking / Lab / Production
    	<input class="filter-check category" type="checkbox" name="country" value="Winemaking / Lab / Production">
    </label>
    </li><li>
    <label class=" container-check">Harvest / Vintage 
    	<input class="filter-check category" type="checkbox" name="country" value="Harvest / Vintage">
    </label>
    </li>
    <li>
    <label class=" container-check">Cellar Door
    	<input class="filter-check category" type="checkbox" name="country" value="Cellar Door">
    </label>
    </li>
    <li>
    <label class=" container-check">General Management
    	<input class="filter-check category" type="checkbox" name="country" value="General Management">
    </label>
    </li>
    <li>
    <label class=" container-check">Sales / Marketing / Retail / Distributor
    	<input class="filter-check category" type="checkbox" name="country" value="Sales / Marketing / Retail / Distributor">
    </label>
    </li>
    </ul>
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在拼接之前,您需要添加检查索引值的条件 另外,我在循环

中添加了数组值

#OR Gate
if aInput == 1 or bInput == 1:
    ORGate = "True"
    ORGateNum = 1
else:
    ORGate = "False"
    ORGateNum = 0

print('OR Gate output is', ORGate, 'or', ORGateNum)

#NOR Gate
if aInput == 1 or bInput == 1:
    NORGate = "False"
    NORGateNum = 0
else:
    NORGate = "True"
    NORGateNum = 1

print('NOR Gate output is', NORGate, 'or', NORGateNum)
        $('.filter-check').change(function(){

        	var selectedFilter = new Array(); // UPDATED
		    var selectedFilterCat = new Array(); // UPDATED

        	$('.country').each(function(){
        		if($(this).is(':checked')){
        			selectedFilter.push(  $(this).val() )	
        			/*console.log(selectedFilter);*/
        		}else{
        			var index = selectedFilter.indexOf( $(this).val() )
        			/*console.log(index)*/
                    
                    if(index >= 0) // ADDED
        			selectedFilter.splice(index, 1 )
        		}
        	})
        		$('.category').each(function(){
        		if($(this).is(':checked')){
        			selectedFilterCat.push(  $(this).val() )	
        		}else{
        			var indexcat = selectedFilterCat.indexOf( $(this).val() )
        			/*console.log(index)*/
                    if(indexcat >= 0)      // ADDED
        			selectedFilterCat.splice(indexcat, 1 )
        		}
        	})
        	
        	
        
        console.log(selectedFilter);
        console.log(selectedFilterCat);
        	
        	//Ajax	
        	var a =	JSON.stringify(selectedFilter);
        	var b =	JSON.parse(a);
        	var c =	JSON.stringify(selectedFilterCat);
        	var d =	JSON.parse(c);
        	$.ajax({
        	  method: "GET",
        	  url: "/wjo/call-job-list.php",
        	  data: { filter: b },
        	  success: function(result){
                $("#joblistings").html(result);
            }
        	})
        	
        })