使用jQuery形式输入多个输入 [英] POST mulitple inputs in form with jQuery
问题描述
我生成一些HTML格式的输入:
I generate some inputs that looks something like this in HTML:
<input id='estemated_days' type='text' data-id='717' value='6'>
<select id='elements_grade' data-id='717'>
<option value='1'></option>
<option value='2'></option>
<option value='3'></option>
</select>
<input id='estemated_days' type='text' data-id='718' value='4'>
<select id='elements_grade' data-id='718'>
<option value='1'></option>
<option value='2'></option>
<option value='3'></option>
</select>
我当时产生约10个,但以两个为例.如何使用jQuery将多个输入值发布到外部PHP文件?以前,我在提交帖子数据时使用过类似的方法(请参见下面的代码),但这似乎仅适用于具有ID的单个字段-不适用于数据库生成的data-id
I generate about 10 at the time but showing you two as an example. How can I POST multiple input values to an external PHP file using jQuery? Previously I have used something like this (see code below) when submiting post data but this only seem to work for single fields with an ID - not a database generated data-id
//Initiate function when user clicks save
$("#save_courseplan").click(function() {
//Store the values of the fields as variables
var elements_grade = $('#elements_grade').val();
var estemated_days = $('#estemated_days').val();
$.post('../update.php', {
estemated_days: estemated_days, //This for example will now represent 6
elements_grade: elements_grade //And this 1
}, function(data) {
$('#updatestatus').html(data); //Display what update.php echoes out
});
});
编辑
好吧,我认为我不太清楚.我已经动态创建了input
字段.这些字段具有从MySQL数据库中获取的data-id
.
数据库看起来像这样:
EDIT
OK I don't think i was quite clear. I have dynamically created input
fields. These fields has a data-id
wich is fetched from a MySQL database.
The database looks something like this:
+------------+------------+
| scpe_id | scpe_days |
+------------+------------+
| 717 | 6 |
+------------+------------+
| 718 | 4 |
+------------+------------+
以前,我已经使用我早些时候在此线程中发布的jQuery代码根据包含的值成功更新了单个字段.一个ID包含一个值,该值是将$_POST
接到我的update.php
文件中,然后插入数据库中.
Previously, I have been successful in updating single field based on the value it contains using the jQuery code i posted earlier in this thread. One ID contains a value, this value is beeing $_POST
to my update.php
file and then inserted in the database.
现在,但是我不能使用ID,因为我需要立即更新多个行,每个行具有不同的数据库ID.
所以我想要的是首先获取data-id
的值,知道要更新的行,然后获取其持有的值以知道要插入的内容-然后为每个<input>
重复.
Now, however I cannot use ID, because I need to update mutiple rows at once, each with different database-ID.
So what I would like is to first fetch the value of the data-id
, to know what row to update, then fetch the value it holds to know what to be inserted - and then repeat for every <input>
.
推荐答案
var queryString = $('form').serialize();
完成.
注释:
- 确保所有表单元素都在表单元素内.
- 所有表单元素必须具有
name
属性.
- Make sure all the form elements, are inside the form element.
- All the form elements must have a
name
attribute .
最终代码可以是:
$.post('../update.php/?' + queryString, function(data) {
$('#updatestatus').html(data);
});
这篇关于使用jQuery形式输入多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!