I'd like to be able to update a page with an AJAX call, where the user can pass some params into input fields, and then 'onclick' data is retrieved from a DB and the page is updated.(我希望能够使用AJAX调用来更新页面,用户可以在其中将一些参数传递到输入字段中,然后从数据库中检索“ onclick”数据并更新页面。)
So essentially I'd like to pass the values a user has entered into certain fields to a jQuery .on(click) event.(因此,从本质上讲,我想将用户输入到某些字段中的值传递给jQuery .on(click)事件。)
These can then be consumed by the route handler so that the results that are displayed from the database are filtered by the user's input.(然后,路由处理程序可以使用这些数据,以便从数据库显示的结果由用户的输入过滤。)
Current set up is below - How do we pass input field data on the button click?(当前设置如下-如何在单击按钮时传递输入字段数据?)
In functions.js:(在functions.js中:)
$(function() {
$('button').on('click', function () {
alert('Your parameters are :'+ $(this).data('value'));
$.ajax({
type: 'GET',
url: '/orders',
success: function(result) {
var html = '';
for (var i = 0; i< result.length; i++) {
html += '<h2>' + result[i].id + ' ' + result[i].product + '</h2>';
}
$('#target').html(html);
}
});
});
});
In ajax.ejs:(在ajax.ejs中:)
<body>
<h1>Ajax</h1>
<input type="text" form="myform" />
<button form="myform" >Get Orders</button>
<div id="target">
</div>
</body>
in orders.js:(在orders.js中:)
module.exports = {
orders: function(req, res){
db.query('select * from orders', function(err,result){
if(err){
console.log(err);
}
res.send(result)
});
},
};
ask by Cheetara translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…