'onclick'不适用于EJS和Node

时间:2018-08-28 23:57:00

标签: javascript html node.js ejs

我正在使用EJS制作一个小型Express应用,并且试图获得一个onclick函数来传递所选选项的值。我对此有2个问题,如下所示。任何帮助将不胜感激!

EJS供参考:

<select class="dropdown" onclick="<%= console.log(this) %>">
    <option disabled selected value> -- Select Environment -- </option>
    <% environments.forEach(env => { %>
        <option value="<%= env %>"><%= env %></option>
    <% }) %>
</select>

1)console.log()发生在页面加载时,但是当我不断在两个选项之间单击时,再也没有真正触发过。

2)所有文档都说this.value应该返回选择的值,但是this返回的似乎是Node对象,与<select>无关。

{ console: [Getter],
  DTRACE_NET_SERVER_CONNECTION: [Function],
  DTRACE_NET_STREAM_END: [Function],
  DTRACE_HTTP_SERVER_REQUEST: [Function],
  DTRACE_HTTP_SERVER_RESPONSE: [Function],
  DTRACE_HTTP_CLIENT_REQUEST: [Function],
  DTRACE_HTTP_CLIENT_RESPONSE: [Function],
  COUNTER_NET_SERVER_CONNECTION: [Function],
  COUNTER_NET_SERVER_CONNECTION_CLOSE: [Function],
  COUNTER_HTTP_SERVER_REQUEST: [Function],
  COUNTER_HTTP_SERVER_RESPONSE: [Function],
  COUNTER_HTTP_CLIENT_REQUEST: [Function],
  COUNTER_HTTP_CLIENT_RESPONSE: [Function],
  global: [Circular],
  process:
   process {
     title: '  - nodemon  app.js',
     version: 'v8.9.3',
     moduleLoadList:
      [ <stuff> ],
     versions: { http_parser: '2.7.0',
  node: '8.9.3',
  v8: '6.1.534.48',
  uv: '1.15.0',
  zlib: '1.2.11',
  ares: '1.10.1-DEV',
  modules: '57',
  nghttp2: '1.25.0',
  openssl: '1.0.2n',
  icu: '59.1',
  unicode: '9.0',
  cldr: '31.0.1',
  tz: '2017b' },
     arch: 'x64',
     platform: 'win32',
     release:
      { <stuff> },
     env:
      { <stuff> },
     pid: 8532,
     features:
      { <stuff> },
     _needImmediateCallback: true,
     execPath: 'C:\\Program Files\\nodejs\\node.exe',
     debugPort: 9229,
     _startProfilerIdleNotifier: [Function: _startProfilerIdleNotifier],
     _stopProfilerIdleNotifier: [Function: _stopProfilerIdleNotifier],
     _getActiveRequests: [Function: _getActiveRequests],
     _getActiveHandles: [Function: _getActiveHandles],
     reallyExit: [Function: reallyExit],
     abort: [Function: abort],
     chdir: [Function: chdir],
     cwd: [Function: cwd],
     umask: [Function: umask],
     _kill: [Function: _kill],
     _debugProcess: [Function: _debugProcess],
     _debugPause: [Function: _debugPause],
     _debugEnd: [Function: _debugEnd],
     hrtime: [Function: hrtime],
     cpuUsage: [Function: cpuUsage],
     dlopen: [Function: dlopen],
     uptime: [Function: uptime],
     memoryUsage: [Function: memoryUsage],
     binding: [Function: binding],
     _linkedBinding: [Function: _linkedBinding],
     _setupDomainUse: [Function: _setupDomainUse],
     _events:
      { warning: [Function],
        newListener: [Function],
        removeListener: [Function],
        SIGWINCH: [Array] },
     _rawDebug: [Function],
     _eventsCount: 4,
     domain: null,
     _maxListeners: undefined,
     _fatalException: [Function],
     _exiting: false,
     assert: [Function],
     config: { target_defaults: [Object], variables: [Object] },
     emitWarning: [Function],
     nextTick: [Function: nextTick],
     _tickCallback: [Function: _tickCallback],
     _tickDomainCallback: [Function: _tickDomainCallback],
     stdout: [Getter],
     stderr: [Getter],
     stdin: [Getter],
     openStdin: [Function],
     exit: [Function],
     kill: [Function],
     argv0: 'node',
     mainModule:
      Module {
        id: '.',
        exports: [Object],
        parent: null,
        filename: <stuff>,
        loaded: true,
        children: [Array],
        paths: [Array] },
     _immediateCallback: [Function: processImmediate] },
  Buffer:
   { [Function: Buffer]
     poolSize: 8192,
     from: [Function],
     alloc: [Function],
     allocUnsafe: [Function],
     allocUnsafeSlow: [Function],
     isBuffer: [Function: isBuffer],
     compare: [Function: compare],
     isEncoding: [Function],
     concat: [Function],
     byteLength: [Function: byteLength],
     [Symbol(node.isEncoding)]: [Function] },
  clearImmediate: [Function],
  clearInterval: [Function],
  clearTimeout: [Function],
  setImmediate: { [Function: setImmediate] [Symbol(util.promisify.custom)]: [Function] },
  setInterval: [Function],
  setTimeout: { [Function: setTimeout] [Symbol(util.promisify.custom)]: [Function] } }

1 个答案:

答案 0 :(得分:1)

<%= console.log(this) %>

这将在服务器上立即执行console.log(this) ,并将该值打印到页面源。

如果您查看呈现的HTML,则会看到onclick="undefined",因为这就是console.log返回的结果。

您想将原始文本console.log(this)呈现到页面上,以便浏览器将其视为(客户端)Javascript代码。