简体   繁体   English

重定向点击 如果 JavaScript 被禁用,否则检查子单选按钮

[英]Redirect on click If JavaScript is disabled, otherwise check the child radio button

I have a radio button within a link:我在链接中有一个radio button

<label class="dd--option">
    <a href="someURL" class="dd--noscript--link">
        <input name="dd_radio" type="radio" class="dd--radio dd--radio--js" checked />
        <span class="dd--label">
            <img class="dd--label--image" src="http://90.0.0.91/show.php/swatch/23080/black"/>
            <span class="dd--label--text">Black</span>
        </span>
    </a>                    
</label>

I want to achieve the following:我想实现以下目标:

  • If JavaScript is disabled on browser, I want to redirect to the link given in href attribute of the anchor tag .如果浏览器禁用了 JavaScript,我想重定向到anchor tag href attribute中给出的链接。
  • If JavaScript is enabled, It should not redirect anywhere.如果启用了 JavaScript,它不应该重定向到任何地方。 It should only check the radio button .它应该只检查radio button

Can I able to achieve it?我能达到吗? If Yes, How ?如果是,如何?

Not 100% sure if the clicking on an <input> will automatically bubble event up to a parent <a> in every browser, but if so, this should do it:不是 100% 确定点击<input>是否会自动将事件冒泡到每个浏览器中的父<a> ,但如果是这样,应该这样做:

$('.dd--noscript--link').on('click', function(e) {
    e.preventDefault();
});

As @Barmar mentioned in the comments, with JS disabled, href="#" would not cause the browser to load a new page.正如@Barmar 在评论中提到的,在禁用 JS 的情况下, href="#"不会导致浏览器加载新页面。

Add a Javascript event handler on the radio button, and use event.stopPropagation() .在单选按钮上添加一个 Javascript 事件处理程序,并使用event.stopPropagation()

$(".dd--radio--js").click(function(e) {
    e.stopPropagation();
});

If Javascript is enabled, this function will be executed, and stopPropagation() will prevent the click from bubbling to the anchor, so it won't follow the link.如果启用了Javascript,就会执行这个函数, stopPropagation()会阻止点击冒泡到锚点,所以它不会跟随链接。

If Javascript is disabled, this code won't be executed, so the click will bubble out to the anchor, and the link will be followed.如果禁用Javascript,则不会执行此代码,因此单击将冒泡到锚点,然后将跟随链接。

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

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