获取选择数据选项并填充隐藏字段

时间:2013-11-12 21:25:43

标签: php jquery mysql database

我需要帮助,我有一个Select,我需要用每个选项的数据(不是值)填充一个隐藏字段,检查下面的代码:

<script type="text/javascript">
        $(document).ready(function($){
        $('#retailer_id').change(function() {
var $selected = $('#retailer_id option:selected');
$('#slug_title').val($(this).val());
});
});
        </script>
<select class="textbox2" id="retailer_id" name="retailer_id">
<option value="">--- Please select store ---</option>
<?php
$sql_retailers = smart_mysql_query("SELECT * FROM cashbackengine_retailers WHERE status='active' ORDER BY title ASC");
while ($row_retailers = mysql_fetch_array($sql_retailers))
{
if ($retailer_id == $row_retailers['retailer_id']) $selected = " selected=\"selected\""; else $selected = "";
echo "<option data-slug_title=\"".$row_retailers['slug_title']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."      </option>";
}
?>
</select>

现在它填充了retailer_id,但我需要获得返回的值(data-slug_title=\"".$row_retailers['slug_title']."\"

EDITED

HTML输出:

<select class="textbox2" id="retailer_id" name="retailer_id">
<option value="">--- Please select store ---</option>
<option data-slug="casas-bahia-teste" value="4">Casas Bahia</option>
<option data-slug="direct-cosmetics" value="6">Direct Cosmetics</option>
<option data-slug="extra" value="3">Extra</option>
<option data-slug="flowers-direct" value="5">Flowers Direct</option>
<option data-slug="gem-motoring-assist" value="1">GEM Motoring Assist</option>
<option data-slug="pc-world-business" value="7">PC World Business</option>
<option data-slug="ponto-frio" value="10">Ponto Frio</option>
<option data-slug="red-house" value="2">Red House</option>
</select>
         <script type="text/javascript">
        jQuery(document).ready( function($) {
        $('#retailer_id').change( function() {
var $selected = $('#retailer_id option:selected');
$('input[name=slug]').val($selected.data('slug'));
});
});
        </script>

0 个答案:

没有答案