Javascript 确认多项更改

标签 javascript

我想提醒用户 4 个字段中所做的任何更改。目前该脚本一次只能运行 1 个。如果进行了 1 或 4 个更改,它只会提醒第一个更改并跳过其他更改。我希望将其全部显示在一个确认框中。

<script>
function validateForm()
{
var w = document.getElementById("item_name");
if (w.value != w.defaultValue)
{
  return confirm('Update Item Name. Continue?');
}

var x = document.getElementById("item_brand");          
if (x.value != x.defaultValue)
{
return confirm('Update Item Brand. Continue?');
}

var y = document.getElementById("department_id");           
if (!y.options[y.selectedIndex].defaultSelected)
{
return confirm('Update Item Department. Continue?');
}

var z = document.getElementById("vendor_part_num");         
if (z.value != z.defaultValue)
{
return confirm('Update Vendor Part Number. Continue?');
}

}
</script>    

最佳答案

您不想单独询问每个字段,因此您可以先检查所有字段,然后询问用户,如下所示

<script>
function validateForm()
{
var strQuestion = '';
var bStChanged = false;
var w = document.getElementById("item_name");
if (w.value != w.defaultValue)
{
  bStChanged = true;
  strQuestion = strQuestion + 'Update Item Name.' + String.fromCharCode(13);
}

var x = document.getElementById("item_brand");          
if (x.value != x.defaultValue)
{
  bStChanged = true;
  strQuestion = strQuestion + 'Update Item Brand.' + String.fromCharCode(13);
}

var y = document.getElementById("department_id");           
if (!y.options[y.selectedIndex].defaultSelected)
{
  bStChanged = true;
  strQuestion = strQuestion + 'Update Item Deparment.' + String.fromCharCode(13);
}

var z = document.getElementById("vendor_part_num");         
if (z.value != z.defaultValue)
{
  bStChanged = true;
  strQuestion = strQuestion + 'Update Vendor Part Number.' + String.fromCharCode(13);
}

if (bStChanged)
{
  return confirm(strQuestion + ' Continue?')
}

}
</script>    

关于Javascript 确认多项更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23237221/

相关文章:

javascript - 避免执行事件的默认操作。 `event.cancelable` 与 `event.preventDefault`?

javascript - 单击按钮后 IE9 中的子窗口将关闭

Javascript/Jquery 将选择列表更改为单选按钮

javascript - JQuery 选色插件

javascript - 未捕获的类型错误 : undefined is not a function

javascript - new Object() 不等于 new function Object 如何使它们相等?

javascript - Twilio 调用自动断开

javascript - Photoswipe JS 第 1358 行初始布局错误

javascript - 如何访问具有无效字符的对象属性

javascript - 替换 HTML 然后将文档片段添加回 html