简体   繁体   中英

On click function fires only on second click

I find few answers on this theme, but none of them helped me.

My on click fires on second click - where is the problem?

<script type="text/javascript">
$(function () {
   $("#back-in-stock-subscribe").click(function () {
      var sku = document.getElementById('back-in-stock-sku-to-get').textContent;
      var action = '@Url.RouteUrl("BackInStockSubscribePopup", new { productSku = "sku" })';
      action = action.replace("sku", sku);

      $("#back-in-stock-subscribe").fancybox({
           'href': action,
           'transitionIn': 'none',
           'width': 500,
           'height': 200,
           'type': 'iframe',
           'centerOnScroll': true
      });
   });
});
</script>

I believe your code on click initializes the fancy box but you need to call the click event on the fancybox itself to launch it.

Try adding .click() at the end:

<script type="text/javascript">
$(function () {
   $("#back-in-stock-subscribe").click(function () {
      var sku = document.getElementById('back-in-stock-sku-to-get').textContent;
      var action = '@Url.RouteUrl("BackInStockSubscribePopup", new { productSku = "sku" })';
      action = action.replace("sku", sku);

      $("#back-in-stock-subscribe").fancybox({
           'href': action,
           'transitionIn': 'none',
           'width': 500,
           'height': 200,
           'type': 'iframe',
           'centerOnScroll': true
      }).click();
   });
});
</script>

Looking at this :

Open fancybox from function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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