Php Javascript/HTML表单字段命名问题
考虑以下简单形式:Php Javascript/HTML表单字段命名问题,php,javascript,html,forms,field,Php,Javascript,Html,Forms,Field,考虑以下简单形式: <form method="GET" action="handle.php"> <input type="hidden" name="action" value="search"> </form> 表单提交由Javascript(iui)在ajax调用中执行。所有字段都从表单中正确收集。然后Javascript希望将ajax调用发送到“form.action” 这就是我的问题开始的地方。对象表单的类型为HTMLFormElemen
<form method="GET" action="handle.php">
<input type="hidden" name="action" value="search">
</form>
表单提交由Javascript(iui)在ajax调用中执行。所有字段都从表单中正确收集。然后Javascript希望将ajax调用发送到“form.action”
这就是我的问题开始的地方。对象表单的类型为HTMLFormElement。表单的action属性应该是字符串类型,并且应该包含“handle.php”。经过几个小时的调试,我注意到form.action现在是HTMLInputElement类型
我的问题:
这是正确的Javascript行为吗?我从来没有想过用表单属性的名称定义表单字段会发生这种情况。同时,我通过对我的领域进行不同的命名来解决这个问题
提前谢谢你的建议
找到了一个简单的方法来显示我的问题。首先是有问题的表格:
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
以及适当的形式:
<form action="test.php">
<input type="hidden" name="NOT_AN_ATTRIBUTE_NAME" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
在第一个窗口中,弹出窗口状态为“[object HTMLInputElement]”,在第二个窗口中:http://localhost/test.php“这似乎是一个错误。也许应该有一个“action”数组
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action[0]);"> //the form action
<input type="button" onclick="alert(this.form.action[1]);"> // the text input
</form>
//形式动作
//文本输入
您的this.form.action
仍然是一个对象。不要使用警报
而是将其放入控制台.log(this.form.action)
中,并使用firebug和firequery尝试发现您的this.form.action
具有哪些事件/属性
!!在使用console.log之前,您需要在firebug中启用控制台。您看到的问题是,表单在JavaScript中是特殊的。它们的所有字段都可以作为属性访问,因此当您使用
this.form.action
时,它将获得字段操作,而不是HTML属性action=“test.php”
尝试更改
警报(this.form.action)
改为alert(this.form.getAttribute('action'))
。请您也添加javascript代码好吗?我不知道这是否适合JS,但从您描述的方式来看,将输入名称从action更改为另一个名称是一个大问题吗?@Chris:显然,我已经通过重命名字段解决了这个问题。但在ajax调用中使用javascript发布表单时,问题仍然存在。表单字段不能与表单属性具有相同的名称。不能仅使用document.getElementById('formid').getAttribute('action')而不是Form.action吗?对于其他字段也是如此?/Matthew:getAttribute似乎可以工作。它没有像这样给出完整的URL。form.action有,但幸运的是ajax并不太在意。@Matt:是的,它提出的解决方案必须包含在Javascript中。