2014-03-14 69 views
1

我希望用戶能夠篩選具有多個類別選擇的項目。 目前,用戶可以從下拉列表中選擇一個類別,並僅查看該類別的項目。我想要做的是,在添加更多下拉菜單後,點擊「添加更多」按鈕,讓用戶選擇更多類別帶多個類別篩選的PHP'添加更多'鏈接

不確定如何爲每個類別添加唯一的ID。任何幫助表示讚賞!

<script> 
function addInput(divName, template){ 
     var newdiv = document.createElement('div'); 
     newdiv.innerHTML = document.getElementById(divName).innerHTML; 
     document.getElementById(template).appendChild(newdiv); 
} 
</script> 
<div id="template"> 
    <select name="category"> 
     <?php 
      $res = mysql_query("SELECT * FROM `categories` ORDER BY `id` ASC"); 
      if(mysql_num_rows($res) > 0){ 
       while($row = mysql_fetch_assoc($res)){ 
        $id = $row['id']; 
        $name = $row['name'];               

        if($cat_id === $id) { 
         echo '<option selected="selected" value="'.$id.'">'.$name.'</option>'; 
        }else{ 
         echo '<option value="'.$id.'">'.$name.'</option>'; 
        } 
       }           
      }         
     ?> 
    </select>           
</div> 
<div id="add_more_cat"></div> 
<a href="javascript:void(0)" onClick="addInput('template', 'add_more_cat');">+ Add more</a> 

回答

0

您可以DIV設置屬性附加傷害。

<script> 
function addInput(divName, template){ 
     var count=0; 
     var newdiv = document.createElement('div'); 
     newdiv.innerHTML = document.getElementById(divName).innerHTML; 
     newdiv.setAttribute("elem_id_"+count,document.getElementById('category').value) ; 
     document.getElementById(template).appendChild(newdiv); 
     count++; 
} 
</script> 

這次沒有測試,但algorytm是:set new attributes新的申報單。然後get these attributes

+0

以及如何將每個「elem_id _」+計數放入顯示結果的php查詢中? – Gadgetster

+0

您可以使用ajax發送元素,也可以使用url發送。 –

+0

你能告訴我你是怎麼做到的嗎? – Gadgetster

-1

您需要添加要創建和使用AJAX保存或提交新類別字段,然後重新裝載您的輸入選擇框,這就是我的理解,請解釋一下你的問題,這將是更多的幫助充滿

+0

即時通訊不知道誰downvote trolling這個網頁,它不是我。 – Gadgetster

+1

有一些無意義的人他們只有這個工作,一些恐怖的人,他們不知道他們在做什麼,他們正在做的充分填滿那裏的慾望:( –

0

這裏是您的解決方案..使用這個JavaScript ..

<script> 
    function addInput(divName, template) { 
    var newdiv = document.createElement('div'); 
    newdiv.innerHTML = document.getElementById(divName).innerHTML; 
    document.getElementById(template).appendChild(newdiv); 

    var selectElements = document.querySelectorAll('select'); 
    for (var i = 0; i < selectElements.length; i++) 
     selectElements[i].id = 'id-' + i; 
     selectElements[i].name = 'select-' + i; 


} 
</script> 
+0

@Gadgetster投票沒有任何理由? –

+0

不是我。 。我怎麼把每個創建的id的下拉菜單選擇並放置在查詢中? – Gadgetster

+0

wait ..檢查編輯..把'name attributes'也放在...所以你可以通過post ... –