gpt4 book ai didi

javascript - 使用javascript收集表单输入值的快捷方式

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:34:44 25 4
gpt4 key购买 nike

我有一个从用户那里收集信息的表单。该表单由 10 个输入文本字段组成。输入文本字段的单独值通过 var first_name = $("#fname").val() 访问作为示例并传递给 var postdata = {'fname':first_name};然后通过 url: "<?php echo base_url(); ?>main_controller/save_data", data: postdata 传递给 ajax然后 Controller 功能将再次收集它。如果我有 30 个输入文本字段然后以这种方式使用,那将花费我很多时间。以微型方式使用 javascript 传递大量输入字段值的最佳方法是什么?这有捷径吗?多谢。我在下面有一个示例代码。

观点:

<input class="input input-large" type="text" name="fname" id="fname" value=""/>
<input class="input input-large" type="text" name="lname" id="lname" value=""/>
<input class="input input-large" type="text" name="address" id="address" value=""/>
<input class="input input-large" type="text" name="age" id="age" value=""/>
<input class="input input-large" type="text" name="height" id="height" value=""/>
<input class="input input-large" type="text" name="gender" id="gender" value=""/>
<input class="input input-large" type="text" name="school" id="school" value=""/>
<input class="input input-large" type="text" name="course" id="course" value=""/>
<input class="input input-large" type="text" name="year" id="year" value=""/>
<input class="input input-large" type="text" name="date_of_birth" id="date_of_birth"
value=""/>

<button class="btn btn-success" id="save" name="save">Save</button>

<script type="text/javascript">
$("#save").click(function(){
var fname = $("#fname").val();
var lname = $("#lname").val();
var address = $("#address").val();
var age = $("#age").val();
var height = $("#height").val();
var gender = $("#gender").val();
var school = $("#school").val();
var course = $("#course").val();
var year = $("#year").val();
var date_of_birth = $("#date_of_birth").val();

var postdata = {
'fname': fname,
'lname': lname,
'address': address,
'age': age,
'height': height,
'gender': gender,
'school': school,
'course': course,
'year': year,
'date_of_birth': date_of_birth
};

$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>main_controller/save_data",
data: postdata,
success: function (data) {
if (data.notify) {
alert('success');
} else {
alert('Failed');
}
}
});

});
</script>

Controller :

function save_data(){
$fname = $this->input->post('fname');
$lname = $this->input->post('lname');
$address = $this->input->post('address');
$age = $this->input->post('age');
$height = $this->input->post('height');
$gender = $this->input->post('gender');
$school = $this->input->post('school');
$course = $this->input->post('course');
$year = $this->input->post('year');
$date_of_birth = $this->input->post('date_of_birth');
$this->insert_model->save_data($fname,$lname,$address,$age,$height,$gender,$school,$course,$year,$date_of_birth);
}

最佳答案

确实有:

$("#save").click(function () {

var postData = $("#idOfYourForm").serialize();

$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>main_controller/save_data",
data: postdata,

// it is perhaps better to specify the dataType
// of the data expected back from the server
// for readability
dataType: 'json',
success: function (data) {
if (data.notify) {
alert('success');
} else {
alert('Failed');
}
}
});

// maybe you need this
return false;
});

http://api.jquery.com/serialize/

关于javascript - 使用javascript收集表单输入值的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com