Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

javascript - 是否可以将参数传递给jQuery .on处理程序?(Is it possible to pass parameters to jQuery .on handler?)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You say "... essentially i'd like to pass the values a user has entered into certain fields to a jQuery .on(click) event."(您说:“ ...本质上,我想将用户输入到某些字段中的值传递给jQuery .on(click)事件。”)

Ok, the way to capture some user input with jQuery would be like this:(好的,用jQuery捕获一些用户输入的方法如下:) var input = $(<element-selector>).val(); You can use that code within the event handling function like this:(您可以在事件处理函数中使用该代码,如下所示:) $('button').on('click', function (e) { var input = $(<element-selector>).val(); $.ajax({ type: 'GET', url: '/orders?param=' + input, 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); } }); }); As you have a GET type of request, you may pass the data appended in the query string, as you see in the url param definition.(当您具有GET类型的请求时,您可以传递附加在查询字符串中的数据,如url参数定义中所示。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...