您好,我正在使用此脚本来防止用户提交特定的空白表单文本输入字段。做得很好,但我在 1 个表单中有 2 个提交按钮,我需要它只为 1 个工作。有什么方法可以使下面的代码使用按钮 ID 或名称应用于 1 个特定按钮?
<script type="text/javascript">
$('form').on('submit', function () {
var thisForm = $(this);
var thisAlert = thisForm.data('alert');
var canSubmit = true;
thisForm.find('[data-alert]').each(function(i) {
var thisInput = $(this);
if ( !$.trim(thisInput.val()) ) {
thisAlert += '\n' + thisInput.data('alert');
canSubmit = false;
};
});
if( !canSubmit ) {
alert( thisAlert );
return false;
}
});
</script>
最佳答案
你的第一行:
$('form').on('submit', function () {
将采用文档中的所有表单元素。您可以将其中的“表单”部分更改为表单元素的 ID,即
$('#form1').on('submit', function () {
关于javascript - 如何仅将 javascript 应用于 1 个表单中的 2 个表单提交按钮中的 1 个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29268612/