Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的更新操作_Javascript_Jquery - Fatal编程技术网

Javascript中的更新操作

Javascript中的更新操作,javascript,jquery,Javascript,Jquery,我是JavaScript新手,我还在学习。所以我有一个关于汽车的对象: var-cars=[ { 图像:'http://superbvehicles.com/wp-content/uploads/2015/10/Nissan-GTR-3.jpg', 名称:'日产', 型号:“全球技术法规”, 惠普:565, 价格:100.000, }, { 图像:'http://bestcarmag.com/sites/default/files/4700070mitsubishi-lancer-06.jpg'

我是JavaScript新手,我还在学习。所以我有一个关于汽车的对象:

var-cars=[
{
图像:'http://superbvehicles.com/wp-content/uploads/2015/10/Nissan-GTR-3.jpg',
名称:'日产',
型号:“全球技术法规”,
惠普:565,
价格:100.000,
},
{
图像:'http://bestcarmag.com/sites/default/files/4700070mitsubishi-lancer-06.jpg',
名称:"三菱",,
型号:'lancer',
惠普:380,
价格:40.000,
},
{
图像:'http://bestcarmag.com/sites/default/files/2048005subaru-impreza-wrx-sti-01.jpg',
名称:'斯巴鲁',
型号:'impreza',
惠普:400,
售价:50.000
},
{
图像:'http://stancewords.stanceworks.netdna-cdn.com/wp-content/uploads/2012/06/datsun-240z-slammed-red.jpg',
名称:'日产',
型号:'fairlady 240z',
惠普:200,
售价:70.000
},
{
图像:'https://s-media-cache-ak0.pinimg.com/736x/35/be/6b/35be6b46846e893d332ddfef989614fe.jpg',
名称:'日产',
型号:'天际线',
惠普:320,
价格:80.000
}
]
以及一个html表,其中填充了来自该对象的信息。我为表中的每一行创建了“编辑”按钮,当您按下该按钮时,它会获取该行的信息并将其插入表单中,以便用户可以对其进行编辑。这是实现以下功能的函数:

功能编辑车(一){
var image=cars[i]。image;
var name=cars[i].name;
var模型=汽车[i]。模型;
var hp=cars[i].hp;
var价格=汽车[i]。价格;
$(“#图像编辑”).val(图像);
$(“#名称编辑”).val(名称);
$(“#模型编辑”).val(模型);
$('hp edit').val(hp);
$(“#价格编辑”).val(价格);
var newImage=$(“#图像编辑”).val();
var newName=$('#name edit').val();
var newModel=$(“#模型编辑”).val();
var newHp=$('#hp edit').val();
var newPrice=$(“#价格编辑”).val();
};
因此,我的问题是如何将新信息(由用户在表单中提供)插入到对象中,以代替旧信息


请注意,如果我的英语不好,很抱歉。

首先,最好将索引存储在某个位置,以便您知道要用新数据替换哪个索引,并且它需要存储在所有单独函数都可以共享的位置-在这种情况下,我们将在全局上下文中定义它。然后,您只需在表单提交时覆盖它:

// Initialise as -1 so we can check that no index is selected
var currentIndex = -1;

function editCar(i){
    // Set the global variable to this index so we can use it in other functions
    currentIndex = i;
    ...
}

// I am assuming your form is wrapped in <form id="form"></form>
$("#form").on("submit", function(event){
    // Without preventDefault, the page might get reloaded and therefor reset
    event.preventDefault();
    // This is for safety, to make sure there is an index
    if(currentIndex >= 0){
        cars[currentIndex].image = $('#image-edit').val();
        cars[currentIndex].name = $('#name-edit').val();
        cars[currentIndex].model = $('#model-edit').val();
        cars[currentIndex].hp = $('#hp-edit').val();
        cars[currentIndex].price = $('#price-edit').val();
    }
});
//初始化为-1,以便检查是否未选择索引
var currentIndex=-1;
功能编辑车(一){
//将全局变量设置为此索引,以便我们可以在其他函数中使用它
currentIndex=i;
...
}
//我假设你的表格是用
$(“#表格”)。关于(“提交”,功能(事件){
//如果没有默认设置,页面可能会被重新加载并因此重置
event.preventDefault();
//这是为了安全,以确保有一个索引
如果(当前索引>=0){
cars[currentIndex]。图像=$(“#图像编辑”).val();
cars[currentIndex]。名称=$(“#名称编辑”).val();
cars[currentIndex]。模型=$(“#模型编辑”).val();
cars[currentIndex].hp=$('#hp edit').val();
汽车[currentIndex]。价格=$(“#价格编辑”).val();
}
});

首先,最好将索引存储在某个位置,以便您知道要用新数据替换的索引,并且它需要存储在所有单独函数都可以共享的位置—在这种情况下,我们将在全局上下文中定义它。然后,您只需在表单提交时覆盖它:

// Initialise as -1 so we can check that no index is selected
var currentIndex = -1;

function editCar(i){
    // Set the global variable to this index so we can use it in other functions
    currentIndex = i;
    ...
}

// I am assuming your form is wrapped in <form id="form"></form>
$("#form").on("submit", function(event){
    // Without preventDefault, the page might get reloaded and therefor reset
    event.preventDefault();
    // This is for safety, to make sure there is an index
    if(currentIndex >= 0){
        cars[currentIndex].image = $('#image-edit').val();
        cars[currentIndex].name = $('#name-edit').val();
        cars[currentIndex].model = $('#model-edit').val();
        cars[currentIndex].hp = $('#hp-edit').val();
        cars[currentIndex].price = $('#price-edit').val();
    }
});
//初始化为-1,以便检查是否未选择索引
var currentIndex=-1;
功能编辑车(一){
//将全局变量设置为此索引,以便我们可以在其他函数中使用它
currentIndex=i;
...
}
//我假设你的表格是用
$(“#表格”)。关于(“提交”,功能(事件){
//如果没有默认设置,页面可能会被重新加载并因此重置
event.preventDefault();
//这是为了安全,以确保有一个索引
如果(当前索引>=0){
cars[currentIndex]。图像=$(“#图像编辑”).val();
cars[currentIndex]。名称=$(“#名称编辑”).val();
cars[currentIndex]。模型=$(“#模型编辑”).val();
cars[currentIndex].hp=$('#hp edit').val();
汽车[currentIndex]。价格=$(“#价格编辑”).val();
}
});

您可以使用
索引
i
)…长时间使用无法更新对象。它只在运行时工作。但是,您可以调用ajax将新表单数据存储在页面中,并从中设计旧对象。您可以使用
索引
i
)…您不能长时间更新对象。它只在运行时工作。但是,您可以调用ajax将新表单数据存储在页面中,并从中设计旧对象。