2012-02-18 59 views
0

任何人都可以帮助我? 我要让表演通过隐藏取决于值jQuery的一些格从两个选择框 这是我的代码由jquery根据2选择框中的2个值显示隐藏div

$("#offer_seeking_select").change(function(){ 
var offer_seeking=$(this).val(); 
var selectedCategory=$("select.category-select:visible").last().find("option:selected").val(); 

$.get(www_base+"adding.php?cID="+selectedCategory+"&offer="+offer_seeking,function(data){ 
if(data.status=="ok"){$("#forms").html(data.html).trigger("forms_content_updated") 
}})}); 

HTML代码

<select id="drop-2"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> 
<select id="drop-3"><option value="offer">Offer</option><option value="seeking">Seeking</option></select> 
<div id="forms"></div> 

和adding.php会显示隐藏一些“格”在从#drop-2和drop-3中选择时取决于数值,例如,如果我从drop-2中选择1并从drop-3中选择show #one和#three hide #two,否则从drop-2中选择2并寻求下降3秀#two和#three hide #one

这个HTML文件adding.php因为我不能在文件adding.php获得CID,并提供

<div id="one">abc</div> 
<div id="two">123</div> 
<div id="three">789</div> 
+1

你可能需要一些时间来清理你的代码,少做字符串concatination。 – Oybek 2012-02-18 11:05:00

回答

0

如何这样的事情,它使用对应于选择的ID类,你可能需要重构,以适应你所需的逻辑,因为我不知道我完全理解。

$('#foo, #bar').change(function(e) { 
    var $elm = $(e.target); 
    var $elmDiv = $('.' + $elm.attr('id')); 

    if ($elm.val() !== "") { 
     $elmDiv.show(); 
    } else { 
     $elmDiv.hide(); 
    } 
});​ 

这里是一个demo

+0

class'category-select'class in two selectbox#drop-2 and#drop-3 – Nguyen 2012-02-18 15:55:45

+0

你可以有多个类,如[here](http://jsfiddle.net/8C8GG/4/) – 2012-02-18 17:21:50

+0

在我的情况下,必须从selectbox#drop-2中选择类别值,然后从selectbox#drop-3中选择类别值,之后必须将变量selectedCategory和offer_seeking传递给adding.php文件,此文件与Mysql一起使用并将结果赋给$ .get(www_base +如果(data.status ==“ok”){$(“#forms”)。html(data.html),那么“added.php?cID =”+ selectedCategory +“&offer =”+ offer_seeking,function(data){ 。 trigger(“forms_content_updated”) }})});并将这个结果中的数据(div#1或div#2)嵌入到div#表格中 – Nguyen 2012-02-19 21:21:32