我只是一个jQuery新手,有人能帮助我吗?
我想创建一个像这样的发票系统
我所需要做的就是添加一个提交按钮它的功能就像这样
。查找由某个唯一id
标识的所有文本区域的输入你的问题有歧义这将为你提供所有文本框的值数组
$(document).ready(function(){
$('#somebuttonid').click(function(){
var tbarray = new Array();
$('.yourtextboxclass').each(function(){
tbarray.push($(this).val()); //You will have a array tbarray with all the values of textboxes
}):
$('#somehiddenelementid').val(tbarray);
$('#somehiddenelementidcontainingform').submit(); //Make sure your action is set,you can access like $_post['$somehiddenelmentname'];
});
});
<!--html side-->
<form id="somehiddenelementidcontainingform" action="youraction" method="post">
<input type="hidden" name="somehiddenelmentname[]" id="somehiddenelementid" />
</form>
我很惊讶地发现,这个函数,用这种方式表达,并没有带回很多有明显答案的帖子。
Amanullah,很多评论要求你先自己试一下,因为人们通常不喜欢回答可以用谷歌搜索的问题。
我本来也想推荐的,但是当我用你的关键词搜索时,没有明显的答案。
要序列化所有的输入元素,可以使用:
$('form :input').serialize()
//创建查询字符串tb1=bob&tb2=smith
$('form :input').serializeArray()
//创建一个JSON对象[{name:"tb1", value:"bob}, {name:"tb2", value:"smith"}]
请注意,这些是根据元素NAME而不是ID。一旦您的值被序列化,您就可以将它们发送到服务器。如果你只想抓取文本框,将选择器更改为$('form :input:text')