gpt4 book ai didi

javascript - jQuery split() 返回未定义

转载 作者:行者123 更新时间:2023-12-03 08:20:33 27 4
gpt4 key购买 nike

我有问题,不知道为什么我在所有“field_input”中看到所有时间“未定义”

当我尝试从 jquery .split 添加时

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="first_name" 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');
});
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

来自“field_include.php”的字符串:

1,first_name,First Name:,First Name,1;1,last_name,Last Name:,Last Name,2;1,email,Email:,Email,3;1,job,Job title:,Job title,4;

我该如何解决?

最佳答案

尝试将 .filter(Boolean) 链接到 fields.split(";") 以删除最后一个索引处的空字符串 "" field 其中字符串最后一个索引处的字符是 ";" ,它将在 var field_input = val.split(', '); as val 将为空字符串 ""

var field = fields.split(";").filter(Boolean);
<小时/>

var fields = "1,first_name,First Name:,First Name,1;1,last_name,Last Name:,Last Name,2;1,email,Email:,Email,3;1,job,Job title:,Job title,4;";

var field1 = fields.split(";");

var field2 = fields.split(";").filter(Boolean);

$.each(field2, function(i, val) {

var field_val = val.split(",");

$("body").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="first_name" 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>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

关于javascript - jQuery split() 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766901/

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