我想提醒用户 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/