Javascript 如何使用ejs获取节点js中复选框的值?
我的代码: Html文件:Javascript 如何使用ejs获取节点js中复选框的值?,javascript,node.js,checkbox,ejs,formidable,Javascript,Node.js,Checkbox,Ejs,Formidable,我的代码: Html文件: <form action="/form" method="post" enctype="multipart/form-data"> Name: <input type="text" name="name" /><br> Email: <input type="email" name="email" /> <br> Age: <input type="number
<form action="/form" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" /><br>
Email: <input type="email" name="email" /> <br>
Age: <input type="number" name="age" /> <br>
Address: <textarea name="address" rows="10" cols="15"> </textarea><br>
Category: <select name="cat">
<option value="1">Php</option>
<option value="2">NodeJs</option>
<option value="3">jQuery</option>
</select><br>
Gender: <input type="radio" name="gen" value="m"/> Male
<input type="radio" name="gen" value="f"/> Female
Hobby: <input type="checkbox" name="hob[]" value="cri"/> cricket
<input type="checkbox" name="hob[]" value="fot"/> football
<input type="checkbox" name="hob[]" value="swi"/> swimming
<input type="submit" value="Submit">
</form>
我使用强大的文件上传,它提供了字段和文件选项。
现在,当提供数据时,上述所有字段都为我提供了正确的输出,但复选框除外,我不知道如何或如何获得输出
为了获得上面的输出,我刚刚输入了->console.log(fields)
您可以通过以下方式访问属性:-
字段。字段名称
。但它不适用于复选框
我想要的是获取要显示给用户的复选框值。复选框可以选择多个值
多谢各位 您可以使用括号表示法
字段['hob[]].
您可以使用括号表示法字段['hob[].].
在.ejs
文件中访问它
Sample form:
<!-- RADIO BUTTONS -->
<p>
<input class="with-gap" type="radio" name="gender" id="male" value="M" checked>
<label for="male">Male</label>
</p>
<p>
<input class="with-gap" type="radio" name="gender" id="female" value="F">
<label for="female">Female</label>
</p>
<button type="submit" class="btn ">Sign up</button>
<form action="/addhobby" method="POST">
<div class="form-group">
<h5>Hobbies</h5>
<input type="checkbox" id="hobby" name="stuhobbies" value="sing"/> <strong>Sing</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="guitarplay"/> <strong>GuitarPlay</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="cricket"/> <strong>Cricket</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="football"/> <strong>Football</strong>
</div>
<button type="submit" class="btn btn-primary btn-md">
Add
</button>
const router = express.Router();
router.post('/addhobby', ensureAuthenticated,(req, res) => {
const { stuhobbies } = req.body;
console.log(stuhobbies);
});
在
.ejs
文件中,我们将介绍以下内容
<form action="/addhobby" method="POST">
<div class="form-group">
<h5>Hobbies</h5>
<input type="checkbox" id="hobby" name="stuhobbies" value="sing"/> <strong>Sing</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="guitarplay"/> <strong>GuitarPlay</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="cricket"/> <strong>Cricket</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="football"/> <strong>Football</strong>
</div>
<button type="submit" class="btn btn-primary btn-md">
Add
</button>
const router = express.Router();
router.post('/addhobby', ensureAuthenticated,(req, res) => {
const { stuhobbies } = req.body;
console.log(stuhobbies);
});
不,这不起作用,它只显示最后选择的值。所以如果我选择板球和足球,它会显示fot。感谢您的尝试。当选择多个时,我会收到检查值数组。但我没有使用多部分。也许这就是区别。在最坏的情况下,你可以为每个复选框使用不同的名称。我喜欢为每个复选框使用不同的名称。但如果我需要根据数据库值生成复选框,那就太糟糕了。但我仍然想使用不同名称的概念。感谢无效的选项,它只显示最后选择的值。所以如果我选择板球和足球,它会显示fot。感谢您的尝试。当选择多个时,我会收到检查值数组。但我没有使用多部分。也许这就是区别。在最坏的情况下,你可以为每个复选框使用不同的名称。我喜欢为每个复选框使用不同的名称。但如果我需要根据数据库值生成复选框,那就太糟糕了。但我仍然想使用不同名称的概念。Thanksur代码“嗜好”必须像name=“hob[cri]”value=“cricket”name=“hob[fot]”value=“football”试试这个(没有测试你的要求)你的代码“嗜好”必须像name=“hob[cri]”value=“cricket”name=“hob[fot]”value=“football”试试这个(没有测试你的要求)