gpt4 book ai didi

jquery - 将 JS 数组传递给 MVC Controller 操作

转载 作者:行者123 更新时间:2023-12-01 00:57:17 26 4
gpt4 key购买 nike

在我的页面中,我有一些输入供用户输入值,然后我将获取所有值并将它们发送到服务器。现在我可以获得值并显示它们,如下所示:

Cylinder: 0.00    Sphere: 0.00    Quantity:1
Cylinder: -0.25 Sphere: 0.00 Quantity:2
Cylinder: -0.50 Sphere: 0.00 Quantity:33
Cylinder: -0.75 Sphere: 0.00 Quantity:2
Cylinder: -1.00 Sphere: 0.00 Quantity:2
Cylinder: -1.25 Sphere: 0.00 Quantity:33
Cylinder: -1.50 Sphere: 0.00 Quantity:4
Cylinder: -1.75 Sphere: 0.00 Quantity:5
Cylinder: -2.00 Sphere: 0.00 Quantity:4

但我不知道如何让他们采取行动进行保存。我正在使用 mvc。

在 View 中我编写了以下 JavaScript:

 var orderModel={};
$(".bulkOrderNumericInput").each(function (index,element) {
if ($(this).val().length!=0) {
orderModel[i]={Cylinder:$(this).attr('valuex'),Sphere:$(this).attr('valuey'),Quantity:$(this).val()};
i++;
}
});

有人可以帮助我吗?

最佳答案

 var orderModel = [];
//loop all the inputs in the page to get values, let's say you give all the inputs a class named'.orderinput'
$('#checkout').click(function(){
$(".orderinput").each(function(){
orderModel.push({Cylinder:$(this).val(),Sphere:blah,Quantity:blah,...);
});
});

//not all values are sotred into the orderModel, the do the post
$.ajax({
url: 'your url',
data:JSON.stringify(orderModel),
type: 'POST',
contentType: 'application/json; charset=utf-8',//this is important!!
success : function(msg) {
//blah..
},
error: function (xhr, ajaxOptions, thrownError) {
//blah...
}
});

关于jquery - 将 JS 数组传递给 MVC Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019786/

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