简体   繁体   English

关于onchange / onfocus的跨浏览器问题

[英]Cross-Browser Issue Regarding onchange/onfocus

I'm trying to turn a <label/> and <input type="radio"/> into a single button that when clicked, adds that specific product to a shopping cart. 我正在尝试将<label/><input type="radio"/>变成单个按钮,单击该按钮会将特定产品添加到购物车中。 This example deals with 1 product, with potentially multiple variants, (ie. The Product would be "Denim Jeans", the Variants would be sizes, "26", "27", "28"). 本示例处理1种产品,可能具有多个变体(例如,该产品将是“牛仔牛仔裤”,这些变体将是尺寸,“ 26”,“ 27”,“ 28”)。

The HTML would look something like, HTML看起来像

<label for="radio_denim26">
  <span>26</span>
  <input type="radio" style="position:absolute;top:-30px;left:-30px;" value="denim26" id="denim26" checked="checked" onfocus="form.submit();" onchange="form.submit();" />
</label>

<label for="radio_denim27">
  <span>27</span>
  <input type="radio" style="position:absolute;top:-30px;left:-30px;" value="denim27" id="denim27" checked="checked" onfocus="form.submit();" onchange="form.submit();" />
</label>

<label for="radio_denim28">
  <span>28</span>
  <input type="radio" style="position: absolute;top:-30px;left:-30px;" value="denim28" id="denim28" checked="checked" onfocus="form.submit();" onchange="form.submit();" />
</label>

**The CSS on the <input/> are to hide the radio buttons from visibility* ** <input/>上的CSS用于隐藏单选按钮以免显示*

This works fine in IE 8, IE 7, and IE 6 (surprisingly!) It also works in Safari/Chrome. IE 8,IE 7和IE 6都可以正常工作(令人惊讶!),而Safari / Chrome也可以工作。 It does not work in Firefox. 工作在Firefox。 When I click a specific variant, let's say "27", it will add the last variant in the group to the cart, that being "28". 当我单击一个特定的变体时,假设是“ 27”,它将把组中的最后一个变体添加到购物车中,即“ 28”。

One final thing to note, if I remove the onfocus="form.submit();" 最后要注意的一点是,如果我删除onfocus="form.submit();" it works fine in Firefox, but no longer in IE 7-. 它在Firefox中工作正常,但在IE 7-中不再有效。

Your radio button tags should probably have "name" attributes on them. 您的单选按钮标记可能应该在其上具有“名称”属性。 The name should be the same for each one, so that they actually work like radio buttons. 每个名称都应相同,以便它们实际上像单选按钮一样工作。

The labels for attribute needs to point to the id of the input. 属性的标签需要指向输入的ID。 It does not matter there placement, they could be hidden or on opposite sides of the screen. 放置位置无关紧要,它们可以隐藏或位于屏幕的相对两侧。 So long as @for of the label points to @id of the input, your good to go. 只要标签的@for指向输入的@id,就可以了。

ie. 即。

<label for="denim1">
    <span>28</span>
</label>
<input type="radio" ... id="denim1" />

Check out w3schools tag label examples 查看w3schools标签标签示例

btw. 顺便说一句 Nice site! 不错的网站! : ) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM