如何更改网格中的每个元素? [英] How to change each element in grid?

查看:83
本文介绍了如何更改网格中的每个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的网格. 如何更改每个元素的索引,即 拥有图像[0],图像[1],图像[2]?

I have grid like this. How to change index for each element, i.e. to have images[0], images[1], images[2]?

<div class="grid">

<div class="gridItem">
  <div><img src="image1.jpg"  class="gridThumb" /></div>
  <input type="hidden" name="images[0]" value="image1.jpg" />
</div>

<div class="gridItem">
  <div><img src="image2.jpg"  class="gridThumb" /></div>
  <input type="hidden" name="images[2]" value="image2.jpg" />
</div>

<div class="gridItem">
  <div><img src="image3.jpg"  class="gridThumb" /></div>
  <input type="hidden" name="images[3]" value="image3.jpg" />
</div>

</div>

这是MVC3站点中创建"视图的一部分. 用户创建模型并上传文件,然后他可以删除其中的一些..然后单击创建". 我需要将图像名称添加到模型中,并将此列表保存在db中.

This is a part of Create view in MVC3 site. User creates model and uploads files, then he can delete some of them.. and then click Create. I need to add image names to the model and save this list in db.

    [HttpPost, ActionName("Create")]
    public ActionResult Create(MyModel m, string[] images)

但是,如果用户删除了某些项,则需要重新索引images [i]元素,因为MVC绑定无法传递缺少元素的数组.

But if user deleted some of items I need to reindex images[i] elements because MVC binding cannot pass array with absent elements.

推荐答案

您可以尝试:

$(function(){
  var gridCnt = 0
  $('.grid').find('.gridItem').each(function(){
    $(this).attr('name','images[' + gridCnt + ']')
    gridCnt++
  })
})

这篇关于如何更改网格中的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆