在我的网络应用程序中,我有编辑个人资料页面。在 editprofile 页面中,它们有很多字段,例如
Name : <input type="text" name="name"/>
Location : <input type="text" class="geolocation" name="location">
Birthdata : <input type="date" name="dob">
Profile Picture : <input type="file" name="photo"/>
我只想发回被编辑的字段的数据,而不是所有字段的数据。
最佳答案
我通常会在后端做这样的事情......但根据您的要求,您可以删除未更改的输入。当您生成 html 时,您可以定义一个额外的数据属性。以下是我将如何做到这一点:
HTML:
<form id="myform">
<input type="text" id="Name" data-initial="Foo" value="Foo" />
<input type="text" id="Location" data-initial="Bar" value="Bar" />
<input type="submit" value="Submit" />
</form>
JS:
$("#myform").submit( function() {
$(this).find("input").filter( function() {
$this = $(this);
return $this.data("initial") == $this.val();
}).remove();
});
http://jsfiddle.net/uLe7T/
我在 fiddle 中添加了一个警报,因此您可以看到它们在提交表单之前已被删除
关于javascript - 提交时从表单中删除未更改的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746530/