条件html提交选项在NodeJs客户端



我的NodeJs应用程序渲染html输出,但我需要传递变量来创建不同的提交选项。

这是现有的工作输出

<!doctype html>                                                                                                                                                               
<html lang="en">                                                                                                                          
<head>                                                                                                                                                                   
</head>                                                                                                                                   
<div class="middle">                                                                                                                                
<form action="/myaction" method="POST">                                                   
<input type="hidden" name="myValue" value="{{newValue}}">
<input type="submit" class="btn btn-default" value="Submit"> 
</form>
</div>

我喜欢使用Javascript检查newValue是否存在(不是Nil)或任何更好的东西if (newValue){…} else {..}

<!doctype html>                                                                                                                                                               
<html lang="en">                                                                                                                          
<head>                                                                                                                                                                   
</head>                                                                                                                                   
<div class="middle">  
if (newValue) {                                                                                                                    
<form action="/myaction" method="POST"  onsubmit="return confirm('Are you sure you want to submit?');">
} else {                                                                                                                                
<form action="/myaction" method="POST">                                                   
}
<input type="hidden" name="myValue" value="{{newValue}}">
<input type="submit" class="btn btn-default" value="Submit"> 
</form>
</div>

我不知道如何使用Javascript在客户端检查NodeJs环境中存在的html条件。

您将需要使用类似EJS(或类似的)的模板库

使用EJS,您可以将值从路由传递到视图,并执行"服务器端"渲染/逻辑。

最新更新