2013-09-25 179 views
0

社區中利用多個變量,我目前有一個select下拉菜單,其值和標題正通過mysql查詢填充。目前,它傳遞的是(source_id)。但是,我希望它也傳遞第二個變量(source_flag)。我需要這兩個原因如下。如果source_flag設置爲'YES',那麼我的JS會使div可見。我需要(source_id)將值插回到我的數據庫中。下面是我目前的下拉和JS功能。添加一點HTML。在選擇選項

HTML

<fieldset style="display: inline;" id="source"> 
<legend class="legend1"><h2>&nbsp; Ticket Source &nbsp;</h2></legend> 
<div style="padding-top: 5px;" id="source"> 
    <div class="input-field"> 
    <?php include $ins_tic.'selectSource'.$ext; ?> 
    </div> 
    <div id="received" style="display: none;"> 
    <input type="text" id="dateTimeField" name="received_on" style="width: 160px;" placeholder="Time Received"/> 
    <script>AnyTime.picker('dateTimeField');</script> 
    </div> 
</div> 
</fieldset> 

的$ ins_tic.'selectSource」。$ EXT文件

<?php 
//This populates the drop down 
echo ' 
<select id="ticket_source" name="ticket_source" tabindex="16" onchange="showEmail(this.value)"> 
<option value="">Select Source</option>'; 
//I want to add source_flag to the query, and add that value to the select option 
$get_sources = mysql_query("SELECT source_id, source_name FROM ticket_source ORDER BY source_name ASC"); 
while (($source_list = mysql_fetch_assoc($get_sources))) 
{ 
     echo ' 
     <option value="'.$source_list['source_id'].'">'.$source_list['source_name'].'</option>'; 
}; 
echo ' 
<option value="0">Other</option> 
</select>'; 
?> 

最後,我目前的JavaScript。請注意,JavaScript目前正在關閉source_id值。我不喜歡這樣做,因爲未來可能會或可能會添加其他來源。

function showEmail(id) 
{ 
    var div = document.getElementById("received"); 
    if(id == 2 || id == 3 || id == 5) 
    { 
     div.style.display = 'block'; 
    } 
    else 
    { 
     div.style.display = 'none'; 
    } 
} 

回答

1

如何:其次

<option value="'.$source_list['source_id'].'" data-flag="'.$source_list['source_flag'].'>'.$source_list['source_name'].'</option>'; 

然後

function showEmail(element) 
{ 
    var id = element.value; 
    var flag = element.options[element.selectedIndex].getAttribute('data-flag'); 
    // Do something with flag... 
    var div = document.getElementById("received"); 
    if(id == 2 || id == 3 || id == 5) 
    { 
     div.style.display = 'block'; 
    } 
    else 
    { 
     div.style.display = 'none'; 
    } 
} 
+0

當我做警報(標誌)

<select id="ticket_source" name="ticket_source" tabindex="16" onchange="showEmail(this)"> 

;它什麼也沒有顯示。是否還有其他需要添加的東西來獲取「」或「YES」的值? –

+0

@CBC_NS你可以展示你的整個代碼嗎?我會爲你提供一些jsfiddle,它應該可以工作。看到源代碼中的標誌是嗎? –

+1

我真的想出了一個解決方案,我用var flag = element.options [element.selectedIndex] .getAttribute('data-flag');來得到標誌的值 –

0

您將需要添加一個分隔符,放在<option>兩個值和解析它背出服務器上。

<option value="'.$source_list['source_id']._.$source_list['source_flag']'">