JQuery - IMG 的访问 ID 附加到 php 脚本的表中
基本上,我有一个表显示几行,旁边有一个删除按钮。当有人单击删除按钮时,我获取该按钮的 ID,将其传递给 php 脚本,从数据库中删除该记录,然后将该行从页面中淡出。这是代码:
$(".remove-button").live('click', function(){
var remove_ptype = encodeURIComponent($(this).attr("id"));
$.ajax({
type: "POST",
dataType : "html",
url: "script.php",
data: "id of remove button goes here",
success: function(msg){
//Do nothing
}
});
$(this).parent().parent().fadeOut(500);
});
好的,下一步。还有一个添加按钮,它会打开一个对话框,然后处理脚本,返回一些数据并为输入的数据追加追加另一行。该脚本还返回删除按钮的 id,然后上面的代码将使用该 id。这是附加的代码:
$("<tr>" +
"<td>" + unescape(name) + "</td>" +
"<td width=\"250\">" + "<img src=\"" + siteurl + "/images/x-button.png\" id=\"" + name_id + "\" class=\"remove-button\" width=\"20\">"+ "</td>" +
"</tr>").appendTo( "#ptypes tbody" );
所以到目前为止效果很好。现在,当我尝试删除这个新添加的行而不刷新页面时,它确实会从屏幕上删除,但我无法获取这个新附加的 .remove-button 的 ID 并将其传递给我的 php 脚本。我知道这是可能的,因为我之前在其他应用程序(例如 Basecamp)中见过它。那么,有人可以指导我如何实现这一目标吗?
仅供参考,我正在使用 JQuerUI 创建对话框等。
非常感谢您的帮助!
添加原始消息
好的,ID 确实没有显示。我已经让它显示出来并且可以工作,但我仍然有一个问题。这是我的 jQUERY 的代码:
$( "#add-type-form" ).dialog({
autoOpen: false,
height: 350,
width: 500,
modal: true,
buttons: {
"Add": function() {
var type_name = encodeURIComponent($('#type_name').attr('value'));
var type_id = '';
if (type_name != "") {
//Submit form
$.ajax({
type: "POST",
dataType : "html",
url: "script.php",
data: "f=1" + "& ff=2" + "MORE STUFF",
success: function(msg){
types_id = msg;
}
});
type_id = types_id;
//Append to display
$("<tr>" +
"<td>" + unescape(type_name) + "</td>" +
"<td width=\"250\">" + "<img src=\"" + siteurl + "/images/x-button.png\" id=\"" + type_id + "\" class=\"remove-type-button\" width=\"20\">"+ "</td>" +
"</tr>").appendTo( "#ptypes tbody" );
$( this ).dialog( "close" );
}},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
所以这是一个 JQUERYUI diagloue,它基本上处理脚本,返回我想要分配给我的 img 标签的 id。问题是,由于某种原因必须按两次添加按钮。如果我删除在 ajax 函数之后为 type_id 变量赋值的行,即:
type_id = types_id;
我无法获取类型 id。如果该行保留在那里,则必须单击添加按钮两次。我不知道为什么会发生这种情况。我确信这是我缺乏 JS 知识,所以我正在寻求帮助,因为我没有发现变量声明本身有任何问题。
再次感谢!
Basically, I've got a table displays a few rows with a delete button next to them. When someone clicks on the delete button, I take the ID of that button, pass it to a php script, delete the record from the database, and then fade the row out from the page. Here's the code:
$(".remove-button").live('click', function(){
var remove_ptype = encodeURIComponent($(this).attr("id"));
$.ajax({
type: "POST",
dataType : "html",
url: "script.php",
data: "id of remove button goes here",
success: function(msg){
//Do nothing
}
});
$(this).parent().parent().fadeOut(500);
});
OK, next step. Also have an add button, which opens up a dialogue, which then processes a script, returns some data and appends appends another row for the data entered. This script also returns an id for the delete button just which would then be used by the above code. Here's the appending code:
$("<tr>" +
"<td>" + unescape(name) + "</td>" +
"<td width=\"250\">" + "<img src=\"" + siteurl + "/images/x-button.png\" id=\"" + name_id + "\" class=\"remove-button\" width=\"20\">"+ "</td>" +
"</tr>").appendTo( "#ptypes tbody" );
So this works beautifully so far. Now when I try to delete this newly added row without refreshing the page, it does get removed from the screen just fine, but I am unable to pick up the ID of this newly appended .remove-button and pass it to my php script. I know it's possible as I've seen it done before in other applications (like basecamp). So, can anyone please guide me on how I can accomplish this?
FYI, I'm using JQuerUI to create the dialogue box, etc.
Thanks so much for your help!
ADDITION TO ORIGINAL MESSAGE
OK so the ID was indeed not showing up. I've got it to show up and it works, but I still have an issue. Here is the code for my jQUERY:
$( "#add-type-form" ).dialog({
autoOpen: false,
height: 350,
width: 500,
modal: true,
buttons: {
"Add": function() {
var type_name = encodeURIComponent($('#type_name').attr('value'));
var type_id = '';
if (type_name != "") {
//Submit form
$.ajax({
type: "POST",
dataType : "html",
url: "script.php",
data: "f=1" + "& ff=2" + "MORE STUFF",
success: function(msg){
types_id = msg;
}
});
type_id = types_id;
//Append to display
$("<tr>" +
"<td>" + unescape(type_name) + "</td>" +
"<td width=\"250\">" + "<img src=\"" + siteurl + "/images/x-button.png\" id=\"" + type_id + "\" class=\"remove-type-button\" width=\"20\">"+ "</td>" +
"</tr>").appendTo( "#ptypes tbody" );
$( this ).dialog( "close" );
}},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
So this is a JQUERYUI diagloue, which basically processes the script, returns the id that I want to assign to my img tag. The trouble is, that the add button has to be pressed twice for some reason. If I remove the line where I assign a value to my type_id varaible after the ajax function, i.e.:
type_id = types_id;
I can't get the type id. If the line stays in there, the add button has to be clicked twice. I'm not sure why that's happening. I am sure that it is my lack of JS knowledge, so I'm seeking help because I don't see anything wrong with the variable declaration per se.
Thanks again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题似乎与您正在做的事情非常相关: jquery Find ID of动态生成的tr标签
提到的代码看起来像这样,但我认为你可以根据你的需要重写它:
如果你运行的是Chrome或Firefox,你甚至能够看到
id
首先是按钮?可能它甚至没有被附加......祝你好运!
您似乎没有等待对查询的响应,这可能就是您必须单击该按钮两次的原因。我已经将脚本的(希望有效)版本拼凑在一起,该版本等待查询完成,然后关闭对话框并执行所有其他发条工作:
也许这会起作用?
This question seems pretty related to what you're doing: jquery Find ID of dynamically generated tr tag
The code mentioned looks like this, but I think you can rewrite it to your needs:
If you're running Chrome or Firefox, are you even able to see the
id
of the button in the first place? It could be that it isn't even being appended...Good luck!
It looks like you are not waiting for a response to your query, which is probably why you have to click the button twice. I've hacked together a (hopefully working) version of your script that waits for the query to finish before closing the dialog and doing all of that other clockwork:
Maybe this would work?