分别读取div中包含的输入值

时间:2017-05-10 01:55:27

标签: javascript jquery arrays json function

我的HTML看起来像这样:

<div id="Section1" class="divFiles">

<input type="text" name="file">

<input type="text" name="file">

<input type="text" name="file">

<input type="text" name="file">

</div>

  <div id="Section2" class="divFiles">      

<input type="text" name="file">

<input type="text" name="file">

<input type="text" name="file">

<input type="text" name="file">

</div>

我想生成一个包含两个属性的对象列表:keylistTitles,key只是div的ID,而listTitles是一个列表input字段中的每个值。我没有分别检索每个div的id有任何问题,但我不确定哪个是正确的方式来读取他们的孩子inputs中包含的所有值。 我的目标是获得这样的结果:

{key: Section1, listTitles: inputValue1Div1, inputValue2Div1, inputValue2Div1, inputValue2Div1 },
{key: Section2, listTitles: inputValue1Div2, inputValue2Div2, inputValue2Div2, inputValue2Div2 }

我使用的代码只检索divs id,问题是使用当前div的输入值字段填充listTitles属性。我需要改变什么?

    var divElements = $(".divFiles");
    var tests = [];
     $.each(divElements, function () {           
     tests.push({ 'key': $(this).attr('id'), 'listTitles':$.each(JSON.parse($(this).children("input[name=file]").val()))});
     })

1 个答案:

答案 0 :(得分:2)

你几乎得到了它。请改用public partial class Startup { public static void ConfigureMobileApp(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); new MobileAppConfiguration() .UseDefaultConfiguration() .ApplyTo(config); config.MapHttpAttributeRoutes(); // Use Entity Framework Code First to create database tables based on your DbContext Database.SetInitializer(new MobileServiceInitializer()); MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings(); config.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore; config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger()); if (string.IsNullOrEmpty(settings.HostName)) { app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions { // This middleware is intended to be used locally for debugging. By default, HostName will // only have a value when running in an App Service application. SigningKey = ConfigurationManager.AppSettings["SigningKey"], ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] }, ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] }, TokenHandler = config.GetAppServiceTokenHandler() }); } app.UseWebApi(config); } }

&#13;
&#13;
.map().get()
&#13;
var divElements = $(".divFiles");
var tests = [];

$.each(divElements, function() {
  tests.push({
    'key': $(this).attr('id'),
    'listTitles': $(this).children("input[name=file]").map(function(i, elem) {
      return this.value;
    }).get()
  });
})

console.log(tests);
&#13;
&#13;
&#13;