dongliao1949 2013-09-25 14:26
浏览 71
已采纳

在select选项中使用多个变量

Community, I currently have a select drop down whose values and titles are being populated via a mysql query. Currently, all it's passing is (source_id). However, I want it to pass a second variable as well, (source_flag). I need both for the following reasons. If the source_flag is set to 'YES', my JS will make a div visible. I need the (source_id) to insert a value back into my database. Below is my current drop down and JS function. Add in a little bit of 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>

The $ins_tic.'selectSource'.$ext file

<?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>';
 ?>

Finally, my current javascript. Note, the javascript is currently going off of the source_id value. I dislike doing it that way because additional sources may and probably will be added in the future.

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

2条回答 默认 最新

  • dongmozhui3805 2013-09-25 14:32
    关注

    How about:

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

    followed by

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

    And then

    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';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗