如何从输入中获取值并将其用作 jQuery 变量?
我不太熟悉 JavaScript/jQuery,但我设法编写了一个民意调查脚本,允许用户对民意调查进行投票并显示结果,而无需刷新页面。这是我想出的 jQuery 代码:
$("#poll_yes").click(function(){
$("#poll_options").html(ajax_load);
$.post(
"query.php?module=vote",
{answer: "yes", pid: "<? display($_GET['username'], poll_id); ?>", to: "<?= $_GET['username'] ?>"},
function(responseText){
$("#poll_options").html(responseText);
},
"html"
);
});
$("#poll_no").click(function(){
$("#poll_options").html(ajax_load);
$.post(
"query.php?module=vote",
{answer: "no", pid: "<? display($_GET['username'], poll_id); ?>", to: "<?= $_GET['username'] ?>"},
function(responseText){
$("#poll_options").html(responseText);
},
"html"
);
});
这将获取页面上输入元素的 id,然后根据该 id 将查询发送到 query.php。然后,query.php 将发送回响应,然后该响应将显示在页面上。
这个脚本运行良好,但我遇到了另一个挑战。我需要在一页上显示多个民意调查。我正在使用 php 从 SQL 数据库生成民意调查,并且想知道是否可以提取“answer: no”、“answer:yes”、“pid: $id”、“to: $username”的值等..来自一个领域。如果可能的话,整个页面是否需要多个表格(针对每个民意调查)?或者一种形式足以满足多个输入?
I'm not very well versed in JavaScript/jQuery, but I have managed to write a poll script that allows a user to vote on a poll and display results without refreshing the page. This is the jQuery code I've came up with:
$("#poll_yes").click(function(){
$("#poll_options").html(ajax_load);
$.post(
"query.php?module=vote",
{answer: "yes", pid: "<? display($_GET['username'], poll_id); ?>", to: "<?= $_GET['username'] ?>"},
function(responseText){
$("#poll_options").html(responseText);
},
"html"
);
});
$("#poll_no").click(function(){
$("#poll_options").html(ajax_load);
$.post(
"query.php?module=vote",
{answer: "no", pid: "<? display($_GET['username'], poll_id); ?>", to: "<?= $_GET['username'] ?>"},
function(responseText){
$("#poll_options").html(responseText);
},
"html"
);
});
This would get the id of the input element on the page and based on the id it would then send a query to query.php. query.php would then send a response back and that response would then be displayed on the page.
This script is working fine but I have run into another challenge. I need multiple polls displayed on one page. I'm using php to generate the polls from a SQL database and was wondering if it was possible to pull the values for "answer: no", "answer:yes", "pid: $id", "to: $username", etc.. from an field. If this is possible, would multiple forms be needed through out the page (for each poll)? or would one form suffice with multiple inputs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为每个民意调查提供一个表单,并将 pid 作为 type="hidden" 的输入包含在内。然后,您可以在所有表单提交事件上附加回调,而不是使用“是”或“否”按钮(通过 ID)的单击。这还可以让您将两个函数折叠成一个回调。
这可以在 http://jsfiddle.net/te9YL/ 进行测试
You could have a form for each poll, and include the pid as an input with type="hidden". Then instead of using the click even on a yes or no button (by ID) you would attach a callback on all forms submit event. This would also let you collapse your two functions into a single callback.
This can be tested at http://jsfiddle.net/te9YL/
是的,您可以使用 jquery 的 .val() 函数轻松获取表单字段的值
yes you can get the value form the form fileds easily by using .val() function of jquery