使用 jQuery 动态添加 div 并通过 AJAX 提交它们
我正在开发的东西遇到了一些阻塞问题。
这一想法是让客户可能拥有多个地址。因此,当您看到客户“个人资料”时,您会看到一个选择框,其中标记了他/她的所有地址,因此您只能看到选定的地址。
它们确实存在,但隐藏在一些 javascript/jquery 中。
因此,添加新地址时就会出现问题。我不知道如何动态地添加更多内容。我所做的是有一个空的隐藏 div,因此,当您按下“添加地址”按钮时,它会显示该 div 并让您填写表单,但我不知道如何添加多个然后保存它们。
问题在于可用性和编程。可用性部分是,您可能已经看到了带有您要添加的地址的 div,但是如果您尝试添加新的 div,该 div 会发生什么情况?应该隐藏吗?如果是这样,应该有一种方法可以让它们回来,以防万一您需要更改一些数据,或者如果您不隐藏它们,您可能会得到一个非常大的滚动条。
然后,有一种方法可以保存每个地址 div 的信息,我将在此处粘贴我的代码:
$('.form-direcciones').each(function(){
var ajData = $(this).serialize();
var idDireccion = $(this).attr('id');
if (idDireccion === 'form-nueva-direccion'){
ajData = ajData + '&action=insertar'+
'&cod_cliente='+cod_cliente;
}
else{
idDireccion = idDireccion.split('-');
ajData = ajData + '&cod_dir='+idDireccion[2] +
'&action=' + action +
'&cod_cliente=' + cod_cliente;
}
$.ajax({
type: "POST",
url: "controllers/direccion.php",
async: false,
data: ajData,
dataType: "html",
cache: false
});
});
如您所见,它会检查 div 的 ID 是否是我用于该目的的 ID,但即使使用该解决方案,它对我来说似乎有点脏。
另外,还有一个问题,就是我的ID重复了...
I'm having some blocking issues with something I'm developing.
The idea is to have customers who may have more than one address. So, when you see the customer "profile" you have a Select box which have all his/her addresses tagged so you only see the one which is selected.
They are really there but hidden with some javascript/jquery.
So, the problem comes when adding new address. I don't know how could I add more dinamically. What I do is to have a hidden div which is empty so, when you press the Add address button it shows the div and let you fill the form however I don't know how to add more than one and then save them.
The issue is with usability and with programming. The usability part is that you may have visible the div with the Address you are about to add but what happens with that div if you try to add a new one? Should be hidden? If so, there should be a way to get them back just in case you need to change some data or if you don't hide them, you may end with a really big scroll bar.
And then, there is the way I save the info for each address div, I'll paste my code here:
$('.form-direcciones').each(function(){
var ajData = $(this).serialize();
var idDireccion = $(this).attr('id');
if (idDireccion === 'form-nueva-direccion'){
ajData = ajData + '&action=insertar'+
'&cod_cliente='+cod_cliente;
}
else{
idDireccion = idDireccion.split('-');
ajData = ajData + '&cod_dir='+idDireccion[2] +
'&action=' + action +
'&cod_cliente=' + cod_cliente;
}
$.ajax({
type: "POST",
url: "controllers/direccion.php",
async: false,
data: ajData,
dataType: "html",
cache: false
});
});
As you may see, it checks if the ID of the div is the one I use for that but even with that solutions it seems a little bit dirty to me.
Also, it comes the problem where I'm having repeated IDs...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个 jsfiddle
http://jsfiddle.net/Z3JPW/12/
我已经完成了让您了解如何在客户端进行操作的基础知识。您可以在客户端完成所有操作,例如编辑、删除和插入,最后将数据保存到服务器端。
Just take a look at this jsfiddle
http://jsfiddle.net/Z3JPW/12/
I have done the basics to give you an idea on how to do stuff client side. You can do everything such as editing and deleting and inserting all at client side and finally take the data to the server side persistence.