javascript - 除非更新表单数据,否则在 Firefox 中禁用表单提交
问题描述
我是一名新手编码员,所以请告诉我如何.. 我有一个更新表单的工作代码,除非文本区域的数据发生更改,否则它会阻止提交表单。
该代码在 chrome 中运行良好,但在 Firefox 中却不行。
$(function() {
$('form')
.each(function() {
$(this).data('serialized', $(this).serialize())
})
.on('change input', function() {
if (event.target.nodeName !== 'SELECT') {
$(this)
.find('input:submit, button:submit')
.attr('disabled', $(this).serialize() == $(this).data('serialized'));
}
})
.find('input:submit, button:submit')
.attr('disabled', true);
});
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.js"></script>
<form action="page.php" method=\ "post\">
<textarea Name="update" cols="50" rows="20">
</textarea> Updation By<span style="font-weight: bold; color: rgb(204, 0, 0);">*</span> :
<select required aria-required="true" name="user">
<option value="" selected="true" disabled="disabled"> - Select - </option>
<option value="user1">User 1</option>
<option value="user2">User 2</option>
<option value="user3">User 3</option>
</select><br><br>
<input name="Submit" type="submit" value="Update">
</form>
请帮助我让它在 Firefox 上运行。
解决方案
控制台错误为“未定义事件”,因此在更改时传递事件
.on('change input', function(event) {
$(function() {
$('form')
.each(function() {
$(this).data('serialized', $(this).serialize())
})
.on('change input', function(event) {
if (event.target.nodeName !== 'SELECT') {
$(this)
.find('input:submit, button:submit')
.attr('disabled', $(this).serialize() == $(this).data('serialized'));
}
})
.find('input:submit, button:submit')
.attr('disabled', true);
});
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.js"></script>
<form action="page.php" method=\ "post\">
<textarea Name="update" cols="50" rows="20">
</textarea> Updation By<span style="font-weight: bold; color: rgb(204, 0, 0);">*</span> :<select required aria-required="true" name="user">
<option value="" selected="true" disabled="disabled"> - Select - </option>
<option value="user1">User 1</option>
<option value="user2">User 2</option>
<option value="user3">User 3</option>
</select><br><br>
<input name="Submit" type="submit" value="Update">
</form>
推荐阅读
- javascript - 使用 html 通过来自 firebase 数据库的 url 显示图像
- mysql - 如果任何行中的字段 X 具有值 Y 则忽略 else group results by GROUP BY in mysql without subquery
- java - 当公共池不支持并行级别 >= 2 时,让 CompletableFuture 为任务创建单独线程的意图
- javascript - 如何处理我的登录页面 PHP 的重定向
- javascript - 在搜索的特定索引处使用 .replace() 的 Javascript
- c - 在最后一个孩子中显示进程树
- c++ - C++ 如何比较 n 个已排序的 int 向量以找到相互元素?
- javascript - 如何检测数组是否具有年份值并定义它?
- python - 规范化 Pandas 数据时加速循环
- graphql - netlify gatsby - 列表小部件上的 markdownRemark