如何更新表单“项目名称”从下拉列表中选择一个项目

发布于 2024-10-29 00:05:38 字数 359 浏览 0 评论 0原文

当选择下拉菜单中的项目时,是否可以更新表单中的隐藏值“项目名称”?

<input type="hidden" name="item_name" value="PHOTOID 2 Product MUG" />

下拉列表:(从 datase 填充。mug

£9.99

a4 print £2.50

ID

当选择一个项目时,但在按下“添加到购物车”按钮之前,可以更新 item_name 值?否则我只会收到发送的照片 到购物车。这会将商品添加到 paypal 购物车,我只能有一个商品名称,然后是下面填充的选项列表,谢谢

Is it possible to update a hidden value "item name" in a form when an item in the drop down menu is selected?

<input type="hidden" name="item_name" value="PHOTOID 2 Product MUG" />

Drop Down List: (populated from datase.

mug £9.99

a4 print £2.50

etc

etc

When an item is selected, but before the add to cart button is pressed it is possible to update the item_name value? Otherwise I just get the photo ID sent to the cart. This adds the item to paypal shopping cart, I can only have one item name then the populated options list below. I hope that makes sense.

Thank in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘世孤行 2024-11-05 00:05:38

如果您的下拉菜单是

<script type="text/javascript">
function updateItemName() {
  var select = document.getElementById('select_stuff');
  var field = document.getElementById('item_name');

  field.value = select.options[select.selectedIndex].value;
};
</script>

<input type="hidden" name="item_name" id="item_name" value="" />

<select name="select_stuff" id="select_stuff" onchange="updateItemName()">
  <option value="Stuff 1" >Stuff 1</option>
  <option value="Stuff 2">Stuff 2</option>
</select>

If your drop-down menu is a <select> tag, then you can do the following:

<script type="text/javascript">
function updateItemName() {
  var select = document.getElementById('select_stuff');
  var field = document.getElementById('item_name');

  field.value = select.options[select.selectedIndex].value;
};
</script>

<input type="hidden" name="item_name" id="item_name" value="" />

<select name="select_stuff" id="select_stuff" onchange="updateItemName()">
  <option value="Stuff 1" >Stuff 1</option>
  <option value="Stuff 2">Stuff 2</option>
</select>
白云悠悠 2024-11-05 00:05:38

你可以用 javascript 来做到这一点,但我不太清楚为什么你需要这样做。如果您有一个带有值的选择和一个隐藏字段,该隐藏字段应该采用与选择相同的值,那么这意味着您将提交相同的数据两次。直接使用select控件的值就可以了。

You could do it with javascript, but I'm not quite sure why you'd need to. If you have a select with values and a hidden field that's supposed to take the same value as the select, then that mean you're submitting the same data twice. Just use the value of the select control directly.

橘香 2024-11-05 00:05:38

用 javascript 很容易做到;对于 jQuery 来说微不足道。假设我们有以下 html:

<ul id="item-list">
   <li>One certain item</li>
   <li>Another one</li>
   ...
</ul>
<input type="hidden" name="item_name" id="item_name" value="" id="the_id"/>

您要做的是:

<script type="text/javascript">
$('#item-list li').click(function() {
    $('#the_id').attr(name, $(this).text());
});
</script>

这意味着:

当用户单击 id item-list 的元素包含的 li 时
分配给 id the_id 的元素,作为名称,他们刚刚单击的 li 包含的文本。


抱歉,我刚刚意识到您想更改名称而不是值:固定。

It's pretty easy to do in javascript; trivial with jQuery. Let's pretend we have the following html:

<ul id="item-list">
   <li>One certain item</li>
   <li>Another one</li>
   ...
</ul>
<input type="hidden" name="item_name" id="item_name" value="" id="the_id"/>

What you would do is:

<script type="text/javascript">
$('#item-list li').click(function() {
    $('#the_id').attr(name, $(this).text());
});
</script>

That means:

as the user clicks on a li contained by the element with id item-list
assign to the element with id the_id, as name, the text contained by the li they just clicked.


Sorry, I just realized you wanted to change the name and not the value: fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文