douchuntang2827 2016-02-27 07:41
浏览 23
已采纳

加载窗口上的jQuery

I have a javascript code, the javascript code is for make font and color preview. So you can choose the font for example Arial and the Color for example red, the code will change a input box with what you select. This selection are automated saved and send it to cart, in the cart I have a button to edit your changes, my problem is here when I press that edit button and I am back at my selections I can't see the preview. For example after I press the edit button, the font must be Arial and the color must be red. I think the jquery is not executed when I return. Is there anyway to solve this?

enter image description here

This is the jQuery code:

jQuery(document).ready(function() {
  jQuery(".product-options dd .font select").change(function() {
    var str = jQuery(this).find("option:selected").text();
    jQuery(".product-options dd input.input-text").css("font-family", str);
  });
  jQuery(".product-options dd .color select").change(function() {
    var str = jQuery(this).find("option:selected").text();
    jQuery(".product-options dd input.input-text").css("color", str);
  });
});
  • 写回答

1条回答 默认 最新

  • dqpfkzu360216 2016-02-27 07:53
    关注

    So you want to call the same code on page load. triggerHandler() is your friend in this case. See below:

    jQuery(document).ready(function() {
      jQuery(".product-options dd .font select").change(function() {
        var str = jQuery(this).find("option:selected").text();
        jQuery(".product-options dd input.input-text").css("font-family", str);
      });
      jQuery(".product-options dd .color select").change(function() {
        var str = jQuery(this).find("option:selected").text();
        jQuery(".product-options dd input.input-text").css("color", str);
      });
    
      // add these two lines:
      jQuery(".product-options dd .font select").triggerHandler('change');
      jQuery(".product-options dd .color select").triggerHandler('change');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程