简体   繁体   English

Turbolink干扰JavaScript

[英]Turbolinks interfering with javascript

I know this is a beat down topic but I tried following the docs and other similar SO posts on this. 我知道这是一个令人沮丧的话题,但我尝试关注此文档和其他类似的文章。

My issue is the JS won't work unless the page is reloaded. 我的问题是,除非重新加载页面,否则JS无法使用。

I have the following JS: 我有以下JS:

$(function() {
  $('a.form-show').click(function(event){
    event.preventDefault();
    $(this).parent().next('div.form-hide').toggle();
  });
});

This toggles a link to appear and hide an element in a form. 这将切换链接以显示和隐藏表单中的元素。

This works only on page reload and not even every time... 这仅适用于页面重新加载,而不是每次都适用。

I tried updating it to (as per SO posts and turbolinks docs): 我尝试将其更新为(根据SO帖子和turbolinks文档):

$(document).on('turbolinks:load', function()){
  $('a.form-show').click(function(event){
    event.preventDefault();
    $(this).parent().next('div.form-hide').toggle();
  });
});

When i use this code, and click the button to toggle, it does nothing but reload the page. 当我使用此代码并单击按钮进行切换时,除了重新加载页面外,它什么也不做。 (at least it runs in the CMD as a reload but nothing on the front end really happens. (至少它作为重载在CMD中运行,但前端实际上什么也没有发生。

And I get this error: 我得到这个错误:

SyntaxError: missing { before function body 语法错误:函数主体前缺少{

I'm no good as JS so this attempt could be completely wrong and I am sure it may be. 我不擅长JS,因此这种尝试可能是完全错误的,而且我相信这可能是错误的。

Any ideas on how to make this work? 关于如何进行这项工作的任何想法?

function()){ - change to function(){ and error function()){ -更改为function(){和错误

SyntaxError: missing { before function body 语法错误:函数主体前缺少{

disappear 消失

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

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