如何使用<a> instead of a button on express.js post?

时间:2017-04-06 12:55:32

标签: javascript node.js twitter-bootstrap express

I am using handlebars as my templating engine and I use buttons to call app.post() on my javascript file.

<form method="POST" action="/smo_assessment">
<div class="container" id="div1">
    <h3 id="header" align="center">Request Inventory</h3>
    <h4 style="color: white">Select the ID of the request you want to make an assesssment</h4>

    <table>
    <td>
        <select class="form-control" style="width: 80px" name="requestID">
            {{#each requests}}
            <option>{{this.id}}</option>
            {{/each}}
        </select>
    </td>
    <td>
        <button buttontype="submit" class="btn btn-default" id=btn1>Make Assessment</button>
    </td>
    </table>
</form>

However, since I am using bootstrap navbar, I need to use a <a> tag instead of a button in order to navigate to a different page.

app.post('/smo_assessment', urlencodedParser, function(req, res){
    connection.query('SELECT * FROM requests WHERE id=?', req.body.requestID, function(err, rows, fields){
        if(err)
            throw err;
        else 
            selectedRequest = rows  

        res.render('smo_assessment', {assessmentType, data: selectedRequest[0]});   
    });

});

2 个答案:

答案 0 :(得分:0)

尝试使用form.submit() Javascript方法:

&#13;
&#13;
<form id="theForm" method="POST" action="/smo_assessment">
<div class="container" id="div1">
    <h3 id="header" align="center">Request Inventory</h3>
    <h4 style="color: white">Select the ID of the request you want to make an assesssment</h4>

    <table>
    <td>
        <select class="form-control" style="width: 80px" name="requestID">
            {{#each requests}}
            <option>{{this.id}}</option>
            {{/each}}
        </select>
    </td>
    <td>
        <a href="javascript:document.getElementById('theForm').submit();" class="linkSubmit" id="linkBtn1">Make Assessment</a>
    </td>
    </table>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在表单中添加一个ID并将其用于点击事件:

<form id="myForm">
  ...
  <a class="btn btn-default" onclick="myForm.submit();">Make Assessment</a>
</form>