it is custom form and i dont know why all time i have "Uncaught ReferenceError: field_inputs is not defined" error
它是自定义形式,我不知道为什么我有“未捕获的ReferenceError:field_inputs未定义”错误
If you also have a way to shorten the code I'd love to know how
如果你还有办法缩短代码,我很想知道如何
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$( ".form" ).change(function() {
$( ".addfromform" ).empty();
field_include();
});
});
function field_include()
{
var form_id = $( ".form" ).val();
$.ajax({
url: 'ajax/field_include.php',
type: 'POST',
data: {
form_id : form_id
},
success: function(data)
{
var fields = data;
var field = fields.split(";").filter(Boolean);
$.each(field, function(i, val){
var field_val = val.split(",");
$( ".addfromform" ).append('<div class="form-group"><label class="col-lg-2 control-label mt10">'+field_val[2]+'</label><div class="col-xs-10"><label for="'+field_val[1]+'" class="field prepend-icon"><input type="'+field_val[0]+'" name="'+field_val[1]+'" id="'+field_val[1]+'" class="gui-input" placeholder="'+field_val[3]+'" autocomplete="off"><label for="'+field_val[1]+'" class="field-icon"><i class="fa fa-user"></i></label></label></div></div>').fadeIn('slow');
if (input_ids != null)
{
var input_ids = ""+input_ids+","+field_val[1]+"";
$(".date").data("input_name", ""+input_ids+"");
}
else
{
var input_ids = ""+field_val[1]+"";
$(".date").data("input_name", ""+input_ids+"");
}
});
}
});
}
function save_guest()
{
var field_names = $(".date").data("input_name");
var field_name = field_names.split(",").filter(Boolean);
$.each(field_name, function(i, input_val){
if(typeof(field_inputs) != "undefined" && field_inputs !== null)
{
var input_date = $( "."+input_val+"" ).val();
var field_inputs = ""+field_inputs+""+input_val+","+input_date+";";
}
else
{
var input_date = $( "."+input_val+"" ).val();
var field_inputs = ""+input_val+","+input_date+";";
}
});
$.ajax({
url: 'ajax/save_new_guest.php',
type: 'POST',
data: {
field : field_inputs
},
success: function(data)
{
document.location.href="../../guest.php";
}
});
}
</script>
<button onclick="save_guest();" class="button btn-primary"> Add guest </button>
I'm new topic AJAX jquery
我是新主题AJAX jquery
1 个解决方案
#1
0
you have to put field_inputs
outside the each
function so that your ajax
can access the variable
你必须在每个函数之外放置field_inputs,以便你的ajax可以访问变量
function save_guest()
{
var field_inputs = null;
var field_names = $(".date").data("input_name");
var field_name = field_names.split(",").filter(Boolean);
$.each(field_name, function(i, input_val){
if(typeof(field_inputs) != "undefined" && field_inputs !== null)
{
var input_date = $( "."+input_val+"" ).val();
field_inputs = ""+field_inputs+""+input_val+","+input_date+";";
}
else
{
var input_date = $( "."+input_val+"" ).val();
field_inputs = ""+input_val+","+input_date+";";
}
});
$.ajax({
url: 'ajax/save_new_guest.php',
type: 'POST',
data: {
field : field_inputs
},
success: function(data)
{
document.location.href="../../guest.php";
}
});
}
#1
0
you have to put field_inputs
outside the each
function so that your ajax
can access the variable
你必须在每个函数之外放置field_inputs,以便你的ajax可以访问变量
function save_guest()
{
var field_inputs = null;
var field_names = $(".date").data("input_name");
var field_name = field_names.split(",").filter(Boolean);
$.each(field_name, function(i, input_val){
if(typeof(field_inputs) != "undefined" && field_inputs !== null)
{
var input_date = $( "."+input_val+"" ).val();
field_inputs = ""+field_inputs+""+input_val+","+input_date+";";
}
else
{
var input_date = $( "."+input_val+"" ).val();
field_inputs = ""+input_val+","+input_date+";";
}
});
$.ajax({
url: 'ajax/save_new_guest.php',
type: 'POST',
data: {
field : field_inputs
},
success: function(data)
{
document.location.href="../../guest.php";
}
});
}