Node.js Jade:在 JavaScript 中使用条件语句

Node.js Jade: Using conditionals within JavaScript

本文关键字:条件 语句 JavaScript js Jade Node      更新时间:2023-09-26

我正在使用Jade模板引擎制作一个Node.js网页。我需要在表中创建行和列,具体取决于输入的值:

- var gpu_count = #{GPU_count};
          for (var g = 0; g < gpu_count; g++)
            tr
              - for (var v = 0; v < 15; v++)
                td.vcardTextValue somevaluegoeshere

#{GPU_count}是我输入到翡翠文件中的一个数字。我希望 for 循环的迭代次数等于 gpu_count。但不幸的是,当我加载页面时,出现了此错误:


Error
    at new JS_Parse_Error (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:196:18)
    at js_error (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:204:11)
    at parse_error (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:296:9)
    at Object.next_token [as input] (C:'Users'Anna'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:528:9)
    at next (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:622:25)
    at vardefs (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:1024:48)
    at var_ (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:1037:27)
    at C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:794:30
    at C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:683:24
    at block_ (C:'Users'whoever'Documents'nodejs'node_modules'jade'node_modules'with'node_modules'uglify-js'lib'parse.js:944:20)
I believe this is because of a syntax error; I cannot place #{GPU_count} inside of the JavaScript. So how would I make a JavaScript variable equal to an input value?

If the variable is in the same scope as the JavaScript, just use it's name, as in:

var gpu_count = GPU_count;

如果您需要的值存储在页面上的输入元素中,如下所示:

<input id="GPU_count"...>

你需要使用一些东西将值从页面发送到服务器(jade正在运行的地方),如AJAX,socket.io 或类似的东西。