【问题标题】:Is there a way to set up a fallback for the <input> formAction attribute in HTML5?有没有办法为 HTML5 中的 <input> formAction 属性设置后备?
【发布时间】:2014-01-03 13:35:05
【问题描述】:

我有一个表单应该动态更改它的提交位置。我通过使用 ajax 引入一个提交按钮来做到这一点,该按钮的 formAction 属性设置为我希望表单提交到的位置(我还引入了许多其他依赖于用户输入的东西,但是作为据我所知,与这个问题无关)。这是我的提交按钮的示例:

<input type="submit" value="Edit" name="save" id="save" formAction="http://example.com" />

formAction 中的 URL 是根据初始表单中的用户输入动态生成的。

在 chrome 和 firefox 中一切正常,但尽管 IE 11 应该支持 formAction,但它似乎忽略了它。无论如何,我希望 IE 9 具有相同的功能,我知道它不支持 formAction,所以我正在尝试创建一个后备。

我过去使用过 Modernizr,但查看 documentation 表明它不检查 formAction 属性。有没有办法在有后备选项的同时使用 formAction 的 html 5 功能?我喜欢尽可能按照 html 5 标准编写代码。

【问题讨论】:

  • 此站点报告 IE9 接受该属性,但它不起作用 (wufoo.com/html5/attributes/13-formaction.html)。您需要为此创建一个 polyfill(转至 Sublime Text 2 并开始破解......)
  • 如果还是动态生成formaction属性,是不是可以同时更改表单上的action属性?
  • @MrLister 是的,我能做到。如果没有其他解决方案可用,那将是我采取的课程。对我来说,这感觉有点像 hack,而我正在努力编写 html 5 标准,并在可能的情况下使用后备。
  • 这就是你在没有 HTML5 的情况下动态设置表单的 action 属性的方式,所以它肯定不是 hack。

标签: jquery html forms modernizr


【解决方案1】:

您可以使用 jQuery 绑定到提交按钮,提取 formAction 并将其应用于 form 元素的操作:

给定以下 HTML

<form action="defaultAction.php" id="myForm">
     <input type="submit" value="save" id="save" formAction="http://example.com/save" />
     <input type="submit" value="delete" id="delete" formAction="http://example.com/delete" />
</form>

您需要在表单上附加一个委托,以监听表单中提交按钮的点击:

var myForm = $('#myForm');
myForm.on('click', 'input[type=submit]', function (e) {
    var attr = this.getAttribute('formAction');

    if (attr) {
        myForm[0].action = attr; //Set the form's action to formAction
        //this.form.action = attr; //As per Greg's comment, this is an equivalent statement to above, and should be used if myForm reference doesn't need to be used elsewhere in your script.
    }
});

由于单击是作为委托应用的,因此任何动态添加到表单的新提交按钮也会触发此单击功能。 (例外如果您将点击处理程序直接绑定到带有e.stopPropagation() 的按钮)

现在,当表单提交时,它将使用formAction 的值作为其操作。

这个jsfiddle 显示了在点击处理程序中检索到的formAction 的值。

编辑实际上,它确实“单击”了默认提交按钮,例如,即使您在焦点文本字段中按下 Enter

【讨论】:

  • 您可以将myForm[0].action = this.formAction 替换为this.form.action = this.getAttribute("formaction")。我不确定formaction 属性将设置什么DOM 属性(如果有)。由于this 指的是一个按钮,this.form 指的是它所属的表单,并且回退到this.getAttribute(...) 可能是使用 pollyfill 的方式。
  • 我已经有了myForm[0] 的引用,它实际上与this.form 相同。它们具有相同的开销,并且都同样可交换。我同意您对使用 getAttribute(...) 的看法,并将其添加到我的答案中。谢谢。
  • 您说myForm[0]this.form 实际上是相同的引用是正确的,但是使用this.form 的优点是不再需要全局变量。
  • @GregBurghardt 在所提出的问题的有限范围内,这是一个很好的观点。如果他需要在其他地方引用myForm,那么将其转换为$('#myForm').on() 并引用this.form 肯定是更好的做法。
  • @crush 这在 IE 11 中不起作用,并且可能在以前的版本中也是如此,尽管我没有机会在 11 之前的任何东西中对其进行测试。我将进一步测试它以确定为什么。
【解决方案2】:

formaction 是一个 HTML5 属性。这就是你使用它的方式

<form action="/url" >
     <input type="submit" value="save1" id="save1" formAction="http://example.com/save1" />
     <input type="submit" value="save2" id="save2" formAction="http://example.com/save2" />
</form>

这是您需要的后备。

if (!('formAction' in document.createElement('input'))){
 $('form').on('click', 'input[type=submit]', function (e) {
    var attr = this.getAttribute('formAction');

    if (attr) {
        this.action = attr; //Set the form's action to formAction
    }
  });
}

【讨论】:

  • 好,但似乎我们必须写 this.form.action = attr; 之类的东西,而不是 this.action = attr;,因为这里 this 指的是输入元素而不是它的父表单!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2021-02-18
  • 2016-07-13
  • 1970-01-01
  • 2011-02-27
  • 2017-09-02
  • 2021-08-15
相关资源
最近更新 更多