我在将代码验证到XHTML 1.0 Strict时遇到了问题。
我一直在使用w.3 validator来尝试验证我的网页。 它告诉我
第112行,第24栏:未指定必需属性“action”
<form id="orderform">
您使用的元素需要上面给出的属性, 但是你已经省略了它。例如,在大多数HTML和XHTML文档中 类型“script”元素和“script”元素是必需的 “img”元素需要“alt”属性。
type的典型值为type =“text / css”for和 用于脚本的type =“text / javascript”。
我是XHMTL和CSS的新手,我现在也在学习Javascript,我已经完成了谷歌搜索,我发现很多人都在谈论使用Javascript线来修复错误,但他们都不够清楚。这里有没有人可以为我提供明确的解释?
这是我的XHTML代码..
<form id="orderform">
<div class="field">
Name:
<input type="text" id="name" name="name" value="" />
</div>
<div class="field">
# of Home shirts:
<input type="text" id="homeshirt" name="home" value="" onchange="updateOrder();" />
</div>
<div class="field">
# of Away shirts:
<input type="text" id="awayshirt" name="away" value="" onchange="updateOrder();" />
</div>
<div class="field">
Date of collection:
<input type="text" id="date" name="date" value="" />
</div>
<div class="field">
Subtotal:
<input type="text" id="subtotal" name="subtotal" value="" readonly="readonly" />
</div>
<div class="field">
Tax:
<input type="text" id="tax" name="tax" value="" readonly="readonly" />
</div>
<div class="field">
Total:
<input type="text" id="total" name="total" value="" readonly="readonly" />
</div>
<div id="button">
<input type="button" class="button" value="Place Order" onclick="placeOrder(this.form);" />
</div>
</form>
和我的Javascript ...
<script type="text/javascript">
function updateOrder() {
const TAXRATE = 0.0925;
const SHIRTPRICE = 39.99;
var numHomeShirt = parseInt(document.getElementById("homeshirt").value);
var numAwayShirt = parseInt(document.getElementById("awayshirt").value);
if (isNaN(numHomeShirt))
numHomeShirt = 0;
if (isNaN(numAwayShirt))
numAwayShirt = 0;
var subTotal = (numHomeShirt + numAwayShirt) * SHIRTPRICE;
var tax = subTotal * TAXRATE;
var total = subTotal + tax;
document.getElementById("subtotal").value = "£" + subTotal.toFixed(2);
document.getElementById("tax").value = "£" + tax.toFixed(2);
document.getElementById("total").value = "£" + total.toFixed(2);
}
function placeOrder(form) {
if (document.getElementById("name").value == "")
alert("I'm sorry but you need to provide a name to print on the shirt.");
else if (document.getElementById("date").value == "")
alert ("I'm sorry but you must provide a date you can collect your shirt(s).");
}
</script>
感谢您的时间,
干杯。 杰米
答案 0 :(得分:1)
也许这太明显了,我错过了什么,但为什么不将action属性添加到表单中?
答案 1 :(得分:1)
不需要javascript。如果你正在使用php,只需添加以下代码:
<form id="your_id" action="<?=$_SERVER['PHP_SELF']?>">
基本上,此代码会将操作设置为同一页面。
或其他人建议,只需添加一个空行动:)
答案 2 :(得分:1)
要修复该特定验证错误,您需要在表单元素中包含action属性。
<form id="orderform" action="">
看起来你正在使用JavaScript来处理表单提交,所以你应该只能将操作保留为空,否则给它一个与表单提交php脚本有关的值。
答案 3 :(得分:1)
XHTML要求您的表单包含一个在按下提交按钮时将被调用的操作(即服务器上的另一个脚本)。
目前您只是在Javascript中实现行为 - 如果禁用此功能,则不会发生任何事情。理想情况下,您应该指向一个脚本,通常是与生成表单的脚本相同的PHP脚本,以处理按下提交按钮:
<form id="your_id" action="<?=$_SERVER['PHP_SELF']?>">
然后你的脚本应该在Javascript没有的情况下处理提交。