0

我有一个从数据库retailer_id 中获取选项的选择输入,我需要它,当管理员在此选择输入上选择一个零售商时,我将使用retailer_slug 获得一个新的,然后,当管理员创建或更新时,将发送到数据库中隐藏输入的retailer_id 和retailer_slug。我的代码是:

已编辑

        <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=\"".$row_retailers['slug_title']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
    }
?>
</select>

<input type="text" name="slug" id="slug" value=""/>
         <script type="text/javascript">
        $(document).ready(function() {
        $('#retailer_id').on('change', function() {
var $selected = $('#retailer_id option:selected');
$('input[name=slug]').val($selected.data('slug'));
});
});
        </script>
4

1 回答 1

1

如果我理解正确,这是您可以做的一件事:

PHP

<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=\"".$row_retailers['retail_slug']."\" value=\"".$row_retailers['retailer_id']."\"".$selected.">".$row_retailers['title']."</option>";
        }
    ?>
</select>
<input type="hidden" name="retailer_slug"/>

jQuery

$('#retailer_id').on('change', function() {
    var $selected = $('#retailer_id option:selected');
    $('input[name=retailer_slug]').val($selected.data('slug'));
});

所以基本上,这里的想法是将retailer_slug值保存为data-每个<option>. 然后,当所选选项发生更改时,该retailer_slug值将复制到隐藏的输入中。

于 2013-11-12T19:16:41.007 回答