如何增加onClick事件中的计数器值?

时间:2019-03-21 17:25:55

标签: javascript counter google-app-maker

对于Google App Maker和编程人员来说,我还很陌生,我正在开发一个应用程序,其中单击了“ +”按钮(onClick事件)时,添加了字段(有点像表中的新行,点击)。我正在尝试建立一个计数器来跟踪正在创建多少个“行”。事实是,既然在App Maker中所有代码显然都必须在onClick事件内进行,所以我无法设置全局变量,并且每次单击按钮时计数器都会重新启动,因此始终为1。

我尝试使用localStorage,但是我不完全知道如何使其工作,因为它仍然会始终保存相同的值。我该如何解决?

  var count = 0;
  count = count +1;
  localStorage.setItem('counter', JSON.stringify(parseFloat(count)));

  textArea.className = 'app-TextArea';
  textArea.style.margin = '8px';
  textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');

  widget.root.descendants.Panel1.getElement().appendChild(textArea);

2 个答案:

答案 0 :(得分:1)

您可以使用页面 custom properties

因此,在页面中,您可以添加 Number 个自定义属性并将其命名为 counter 。 然后您的代码应如下所示:

  var count = widget.root.properties.counter || 0;
  count += 1;
  widget.root.properties.counter = count;

  textArea.className = 'app-TextArea';
  textArea.style.margin = '8px';
  textArea.setAttribute("placeholder", "Follow up # " + count);

  widget.root.descendants.Panel1.getElement().appendChild(textArea);

答案 1 :(得分:0)

您应该首先从scproxy获取数据,然后对其进行递增,最后将新值设置为localStorage

localStorage