jQuery - 根据单击的按钮将变量/数据传递给 div
我有一些来自数据库的数据循环出现。每条记录都有一个名为“兑换”的按钮(实际上是超链接)。用户可以单击此按钮来确认他的选择。如果他单击“是”,则会出现另一个对话框,要求他单击“打印”按钮或取消打印。如果他单击“打印”,则属于该记录的变量/数据应发送到 ID 为“printing_div”的 div。这些变量将用于查询数据库,检索某些结果,然后回显结果,以便用户可以打印该数据。
为了便于说明,请考虑以下情况:
Record 1 ----- <a href="#" id="r1">Redeem</a>
Record 2 ----- <a href="#" id="r2">Redeem</a>
Record 3 ----- <a href="#" id="r3">Redeem</a>
.
...
假设用户单击属于第二条记录的“兑换”按钮,则: 1. 弹出一个对话框,询问他是否要兑换。可点击的按钮有“是”和“否”。如果按“否”,该对话框就会消失。如果单击“是”,则会启动另一个对话框,其中显示:您确定要立即打印吗?可单击的按钮是“立即打印”和“取消打印”。如果单击“取消打印”,则关闭最近的对话框。如果单击“立即打印”,则所单击的超链接的 ID(即“r2”)应作为值传递给 ID 为“printing_div”的 div。该 r2 将用于查询数据库,例如:
$query = "SELECT FROM table WHERE col = 'r2' ";
$row = mysql_fetch_array($query);
echo $row[0].$row[1];
将打印上述回显的数据。
因此,就我而言,我无法将 id(即 r2)传递给printing_div,以便我可以查询数据库。我该怎么做?
感谢所有帮助。谢谢。
I have some data from database coming out in a loop. Each of these records have a button (hyperlink actually) called "Redeem". A user can click on this button to confirm his selection. If he clicks yes, then another dialog will show up asking him to click on the "Print" button or cancel printing. If he clicks "Print", then the variables/data belonging to this record, should be sent over to a div with ID: "printing_div". These variables will be used to query the database, retrieve certain results and then echo the result so that the user can print this data.
For illustration, consider below:
Record 1 ----- <a href="#" id="r1">Redeem</a>
Record 2 ----- <a href="#" id="r2">Redeem</a>
Record 3 ----- <a href="#" id="r3">Redeem</a>
.
...
Say if user clicks on the Redeem button belonging to the 2nd Record, then:
1. A dialog box pops up asking him to confirm if he wants to Redeem. Clickable buttons are "Yes" and "No". If No is pressed, the dialog box disappears. If Yes is clicked, then it launches another dialog box which says: Are you sure you want to print now? Clickable buttons are "Print Now" and "Cancel Print". If "Cancel Print" is clicked, the recent dialog box is closed. If "Print Now" is clicked, then the id of that hyperlink that was clicked (i.e 'r2') should be passed as a value to a div with ID 'printing_div'. This r2 will be used to query database such as:
$query = "SELECT FROM table WHERE col = 'r2' ";
$row = mysql_fetch_array($query);
echo $row[0].$row[1];
The above echo'ed data will be printed.
So in my case, I am unable to pass the id (i.e r2) to the printing_div, so that I can query the database. How can I do this?
All help is appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,给定您的链接,如下所示:
以及您提到的用于打印的 div:
您可以像上面提到的 jfriend00 一样:
然后当然您可以通过以下方式在发送回服务器的任何 AJAX 调用中检索新属性:
希望有所帮助。
So given your links, like so:
And a div you mentioned for printing:
You can do like jfriend00 mentioned above:
And then of course you can retrieve the new attribute in any AJAX call you send back to the server by:
Hope that helps.
有几种不同的方法来传递变量。在您的示例中,您在经历 2 个对话框时将一个变量从 a 点传递到 b 点。我通过在一个对话框中使用两个 div 并根据对话框中的点击隐藏和显示 div 来简化您的对话框。
然后我使用 jQuery 的数据函数 将值存储在 dom 上的特定位置。我经常看到隐藏的表单字段也使用了特定的 id。
主要思想是你“通常”不能将它作为函数传递给点击处理程序,但你可以将它存储在 dom 中并在需要时调用它。
这是 Jquery 脚本
这是 HTML
There are a couple different ways to pass along variables. In your example you're passing along one variable from point a to point b while going through 2 dialogs. I simplified your dialog's by using two divs in one dialog and hiding and showing the divs according to the clicks in the dialog.
I then used jQuery's data function to store the value in a specific spot on the dom. I've often seen hidden form fields with a specific id used as well.
The main idea is you "generally" can't pass it as a function to a click handler, but you can store it in the dom and recall it when you need it.
Here is Jquery Script
Here is HTML
如果你有一个像这样的链接:
并且你在 jQuery 中有一个点击处理程序,如下所示:
If you have a link like this:
and you have a click handler in jQuery for it like this: