Javascript:在提交之前快速检查表单内容是否已更改 [英] Javascript: fast way to check if form contents have been changed before submission

查看:121
本文介绍了Javascript:在提交之前快速检查表单内容是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题(我猜)...
我是一个编辑元素的html表单。
作为页面加载,表单填充了当前元素属性(例如名称等)
我可以修改一些输入字段,甚至决定不这样做但我在提交按钮上单击相同因为是唯一可用的。

Simple question (I guess)... I'm in an "edit-some-element" html form. As the page load, the form is filled with current element attributes (eg. name, etc.) I could modify some input fields or even decide not to do that but I click the same on submit button because is the only available one.

我的问题:
是否有输入元素的属性/属性包含我可以访问的初始输入值来比较当我点击提交按钮时,他们与当前的?
换句话说:
是否可以快速检查在提交表单之前是否至少有一个输入字段已被更改? (所以如果不是真的需要更新就停止事件)
(我的意思是真的改变了,当我修改某些东西但不像以前那样重写它时不考虑改变)

MY QUESTION: is there a property/attribute of the input elements containig the initial input values that I can access to compare them with the current ones when I click on submit button? In other words: is there a fast way to check if at least one input field has been changed before submitting the form? (so to stop the event if an update isn't really necessary) (and I mean "really changed" not considering as changes when I modify something but rewrite it as it was before)

我想要一个最小的例子:
如何检查一个简单的输入文本名称是否已被更改

I'd like a minimal example: How to check if a simple input-text "name" has been changed

PS
当然,要触发check-if-something-changed函数,
如果我决定使用纯javascript,我会在使用jQuery时使用onSubmit事件,我会使用类似的东西:

P.S. Of course, to trigger the "check-if-something-changed" function, if I decide to use pure javascript I would use onSubmit event while using jQuery I would use something like:

$('#formMod').submit(function(){ ...

谢谢。

推荐答案

您可以使用输入元素< a href =http://www.w3schools.com/jsref/prop_text_defaultvalue.asp> defaultValue dom property:

You can use the input element defaultValue dom property:

function validateInput(input) {
    if (input.value == input.defaultValue) {
        alert("Please change field.");
        return false;
    }
    return true;
}

这篇关于Javascript:在提交之前快速检查表单内容是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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