gpt4 book ai didi

javascript - 选择表单后从数据库获取隐藏的输入值

转载 作者:行者123 更新时间:2023-11-29 13:27:27 25 4
gpt4 key购买 nike

我有一个选择输入,可以从数据库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>

最佳答案

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

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值被复制到隐藏输入。

关于javascript - 选择表单后从数据库获取隐藏的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19937302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com