将简单的 html 列表与表单中的下拉列表同步
我想同步一个简单的html列表,就像下面的一个表单中的下拉列表一样,
<div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="#" id="current">Item one</a></li>
<li><a href="#">Item two</a></li>
<li><a href="#">Item three</a></li>
<li><a href="#">Item four</a></li>
<li><a href="#">Item five</a></li>
</ul>
</div>
#navlist li
{
display: inline;
list-style-type: none;
padding-right: 20px;
}
这意味着,对于列表中的任何选定元素,使用javascript,我希望能够在一侧记住选定的项目,另一方面更新下拉列表中的元素。有可能吗?
谢谢你!
编辑:
有了这个代码,我想在选择产品时在选择列表中显示每个产品的库存最大值
<script>
$(function ()
{
var $select = $('#mySelect');
$('#navlist li a').live('click', function ()
{
$select.val($(this).text());
});
});
</script>
<form name="add-to-basket" method="post" action="<?= Route::url('Add to Basket', array('sale_id' => $sale->id)); ?>">
<? foreach ($types as $type):?>
<div id="navcontainer">
<ul id="navlist">
<li value = "<?=$type->stock_2 ?>"><a href="#"><?= $type->label;?><?= $type->stock_2;?></a></li>
</ul>
</div>
<? endforeach; ?>
<select name="number" id="mySelect">
<? for ($i = 1; $i <= $type->stock_2; $i++): ?>
<option <?= $type->stock_2 == $i ? 'selected="selected"' :''?> value="<?= $i ?>"><?= $i ?></option>
<? endfor; ?>
</select>
</form>
i want to synchronise a simple html list, like the one below with a drop down list in a form
<div id="navcontainer">
<ul id="navlist">
<li id="active"><a href="#" id="current">Item one</a></li>
<li><a href="#">Item two</a></li>
<li><a href="#">Item three</a></li>
<li><a href="#">Item four</a></li>
<li><a href="#">Item five</a></li>
</ul>
</div>
#navlist li
{
display: inline;
list-style-type: none;
padding-right: 20px;
}
meaning that, for any selected element from the list, using a javascript, i want to be able on one side to memorise the selected item, and on the other part to update the elements in a drop down list. is it possbile?
thank you!
edit:
having this code, i want to display for each product the maximum value of his stock in the select list, when the product is selected
<script>
$(function ()
{
var $select = $('#mySelect');
$('#navlist li a').live('click', function ()
{
$select.val($(this).text());
});
});
</script>
<form name="add-to-basket" method="post" action="<?= Route::url('Add to Basket', array('sale_id' => $sale->id)); ?>">
<? foreach ($types as $type):?>
<div id="navcontainer">
<ul id="navlist">
<li value = "<?=$type->stock_2 ?>"><a href="#"><?= $type->label;?><?= $type->stock_2;?></a></li>
</ul>
</div>
<? endforeach; ?>
<select name="number" id="mySelect">
<? for ($i = 1; $i <= $type->stock_2; $i++): ?>
<option <?= $type->stock_2 == $i ? 'selected="selected"' :''?> value="<?= $i ?>"><?= $i ?></option>
<? endfor; ?>
</select>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://jsfiddle.net/stofke/gukh2
我希望这不是你想要的,因为如果其他人当另一个人购买一件物品时,JavaScript 将无法准确计算物品数量。在我看来,这似乎不仅仅是 javascript 的工作。
HTML
CSS
JavaScript
http://jsfiddle.net/stofke/gukh2
I hope it's not this what you want because if someone else buys an item while another person is doing the same, the javascript will not have the accurate count of items. This does not seem to me a javascript only job.
HTML
CSS
JavaScript
尝试一下这个功能:
您需要将其添加到每个列表项的onclick事件中:
您还需要添加一个下拉框:
Try out this function:
You need to add it to the onclick event of each list item:
You also need to add a dropdown box: