如何在jquery中使用for循环
如何在jquery中使用for循环在MVC4应用程序的隐藏输入字段中编辑和存储多个编辑过的值 这是我的Jquery如何在jquery中使用for循环,jquery,asp.net-mvc,Jquery,Asp.net Mvc,如何在jquery中使用for循环在MVC4应用程序的隐藏输入字段中编辑和存储多个编辑过的值 这是我的Jquery <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(functio
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('p').click(function () {
var textbox = $('<input id="Text1" type="text" name="Name" />')
var oldText = $(this).text();
$(this).replaceWith(textbox);
textbox.blur(function () {
var newValue = $(this).val();
var listItems = $('.listItem');
listItems.each(function () {
$(this).replaceWith(
$('<p/>', { text: newValue })
.after(
$('<input />', { type: 'hidden', name: 'Name', value: newValue })
)
);
});
});
textbox.val(oldText);
});
});
</script>
$(文档).ready(函数(){
$('p')。单击(函数(){
变量文本框=$('')
var oldText=$(this.text();
$(此).replacetwith(文本框);
textbox.blur(函数(){
var newValue=$(this.val();
变量listItems=$('.listItem');
listItems.each(函数(){
$(此)。替换为(
$('',{text:newValue})
.之后(
$(“”,{type:'hidden',name:'name',value:newValue})
)
);
});
});
textbox.val(旧文本);
});
});
如果要在jquery中循环任何集合,请使用each()
。为什么要为设置
$.each(collectionarray, function( index, value ) {
// your code
});
假设您有一个模型:
public class User
{
public string Name { get; set; }
public string Address { get; set; }
}
然后控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var user = new User
{
Name = "test1",
Address = "add1"
};
return View(user);
}
[HttpPost]
public ActionResult Index(User user)
{
return Content(
string.Format(
"name: {0}, address: {1}",
user.Name,
user.Address
)
);
}
}
还有一个观点:
@model User
@using (Html.BeginForm())
{
<ul class="editor">
<li>
@Html.LabelFor(x => x.Name, Model.Name)
@Html.EditorFor(x => x.Name)
</li>
<li>
@Html.LabelFor(x => x.Address, Model.Address)
@Html.EditorFor(x => x.Address)
</li>
</ul>
<button type="submit">OK</button>
}
最后,为了最初隐藏输入字段,您可以定义我应用于
元素的.editor
CSS规则:
.editor input {
display: none;
}
不太确定问题是什么,for循环做什么?它在哪些领域循环?为什么我们要使用循环?我有一个模型类,我被分配了名称,地址,pincode,我使用该jquery编辑这些多个字段时在视图中显示的pincode我想将所有编辑的字段存储在该页面上,并在MVC4应用程序的下一个视图中显示我尝试过它不起作用我无法存储多个编辑的字段值例如我分配了Name=Danny Addres=Chicago我想在我的翻页并将两个编辑的值传递给下一个ViweHi Darin我为名称1和地址2的属性分配了一个id,因此当我编辑名称时,我想显示名称的id和类似的对象地址,但在上面的示例中,我无法将id传递给控制器类
.editor input {
display: none;
}