簡體   English   中英

從console.log獲取值到html

[英]Getting value from a console.log to html

我寫的函數工作正常,值正確寫入我的終端onSubmit,但是我很難拼湊為什么這段代碼沒有更新我的html。

router.post('/index', function(req, res, next) {

  // ...Get form values, do calculations to get toCost value.

      console.log('$' + toCost);

      $(document).ready(function() {
        var output = document.getElementById('cost');
        output.innerHTML = toCost;
      });

  res.location('/');
  res.redirect('/');
})

我收到以下錯誤,jquery正在加載正常。

$ is not defined

不可否認,這是一個過度設計的節點/快遞應用程序。 代碼片段包含在郵件請求中的路由文件中。

如何將我的價值放到html上?

玉的布局

extends layout

block content
  form(...)
  div#cost

通常的方法如下:

router.post('/index', function(req, res) {   <<< 'next' removed (only needed if using middleware)

  // ...Get form values, do calculations to get toCost value.

  console.log('$' + toCost);

  res.render('indexTemplate', {cost: toCost}); <<< pass toCost through to jade as 'cost'
});

然后在indexTemplate的jade文件中,您可以像這樣引用傳入的'cost'變量:

indexTemplate.jade:

extends layout

block content
  form(...)
  div#cost #{cost} <<< adds the cost variable to page

然后,toCost的值將出現在結果頁面中。

服務器端不需要jQuery。

jQuery是為DOM操作而制作的,服務器上沒有DOM。 嘗試使用'jsdom'。 jQuery是為客戶端腳本編寫的,而不是服務器端客戶端操作。

您似乎正在嘗試從后端在客戶端進行更改。 它不起作用。

您可以在后端創建一個變量來存儲計算結果。 並通過從客戶端get查詢來獲取此值

你想從服務器端的NodeJS到達DOM(在客戶端)。 它不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM