jQuery split()返回undefined

时间:2015-11-17 21:00:16

标签: javascript php jquery

我有一个问题,我知道为什么蝙蝠我会一直看到" undefined"在所有" 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');
      });
    }
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

字符串来自&#34; field_include.php&#34;:

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;

我该如何解决?

1 个答案:

答案 0 :(得分:1)

尝试将.filter(Boolean)链接到fields.split(";")以删除最后一个索引""的空字符串field,其中字符串的最后一个索引为";",这将返回undefined var field_input = val.split(','); 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>
static void Main(string[] args)
{
    WindsorContainer container = new WindsorContainer();
    var dependencies = new Dependency[2];
    dependencies[0] = Dependency.OnValue("x", "xV");
    dependencies[1] = Dependency.OnValue("y", "yV");
    container.Register(Component.For<IBla>().ImplementedBy<Bla>().DependsOn(dependencies));
    var bla = container.Resolve<IBla>();
}

public interface IBla 
{
}

public class Bla : IBla
{
    readonly string _x;
    readonly string _y;

    public Bla(string x, string y)
    {
        _x = x;
        _y = y;
    }
}