2016-08-11 27 views
-1

我的html页面有选择菜单来选择usertype,min agemax age。我需要选择选项并将值作为数组对象传递给ajax。当我加载页面getAjax功能正在工作。但是当我在选择菜单上选择任何选项时,它不起作用。如何用一个jquery函数获取少数选择值

<script type = "text/javascript" > 
 
    $(document).ready(function() { 
 
    var ajaxUrl = '<?php echo base_url(); ?>' + 'main/search'; 
 

 
    var dataPara = { 
 
     usertype: 1, 
 
     ageSelectStart: 21, 
 
     ageSelectEnd: 35 
 
    }; 
 

 
    getAjax(ajaxUrl, dataPara); //working 
 
    //following code is not working 
 
    $("#usertypeSelect", "#ageSelect1", "#ageSelect2").change(function() { 
 
     dataPara[$(this).attr('id')] = parseInt($(this).val()); 
 
     getAjax(ajaxUrl, dataPara); 
 
    }); 
 

 
    }); 
 

 
function getAjax(URL, dataPara) { 
 
    $.ajax({ 
 
    url: URL, 
 
    data: dataPara, 
 
    dataType: "html", 
 
    type: "POST", 
 
    success: function(retdata) { 
 
     $("#mainData").html(retdata); 
 
    } 
 
    }); 
 
} </script>
<html> 
 
<div class="side-menu-container"> 
 
    <ul class="nav"> 
 
    <li> 
 
     <label>User type</label> 
 
     <select id="usertypeSelect"> 
 
     <!-- options here--> 
 
     </select> 
 
    </li> 
 
    <li> 
 
     <label>Age</label> 
 
     <select id="ageSelect1"> 
 
     <!-- options here--> 
 
     </select> 
 
     <label>To</label> 
 
     <select id="ageSelect2"> 
 
     <!-- options here--> 
 
     </select> 
 
    </li> 
 
    </ul> 
 
</div> 
 

 
</html>

+0

尝试'$( “#usertypeSelect,#ageSelect1,#ageSelect2”)change' –

回答

1

对于你需要把所有的选择中一个单引号多重选择器。 Here是一个链接

$("#usertypeSelect,#ageSelect1,#ageSelect2").change(function() { 
     // Rest of code 
    }); 

最初也dataPara对象不包含ageSelect1 & ageSelect2。他们只会被附加在相应的变化select

0

我相信在jQuery中,要选择一个行中的多个类/ ID,他们仍然必须在同一组引号。因此该行:

$("#usertypeSelect", "#ageSelect1", "#ageSelect2").change(function() { 

应改为阅读:

$("#usertypeSelect, #ageSelect1, #ageSelect2").change(function() { 
0
$("#usertypeSelect, #ageSelect1, #ageSelect2").on('change', function() { 
    dataPara[$(this).attr('id')] = parseInt($(this).val()); 
    getAjax(ajaxUrl, dataPara); 
}); 

试试这个

+0

虽然这可能会回答这个问题,请提供任何解释,以确保人们了解这里发生的事情。 – Randy

相关问题