2013-09-25 70 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']'">