使用HTML按钮而不是输入类型使用Form元素提交排除吗? [英] Does using an HTML Button instead of an input type submit preclude using the Form element?

查看:87
本文介绍了使用HTML按钮而不是输入类型使用Form元素提交排除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML:



I had HTML like this:

<div class="floatleft">
  <h2>Add a Job/Location</h2>
  <form>
    <label for="jobLoc">Designation for Job/Location</label>
    <input type="text"  id="textJobLoc" name="textJobLoc" autofocus>
    <input type="submit"  id="insertJobLoc" name="insertJobLoc" value="Save Job/Location">
  </form>
</div>





...但后来认为我没有/需要表单提交行为并使用HTML按钮代替提交类型的输入元素:





...but then thought I didn't/need want the form submit behavior and used an HTML button instead of an input element of type submit:

<div class="floatleft">
  <h2>Add a Job/Location</h2>
    <label for="jobLoc">Designation for Job/Location</label>
    <input type="text"  id="textJobLoc" name="textJobLoc" autofocus>
    <button type="button" id="btnSaveJobLoc" name="btnSaveJobLoc">Save Job/Location</button>
</div>





但由于它现在不是表单提交,保留表单标签是否有意义?是否有损害/帮助保留/删除表单标签?



But since it's now not a "form submit", does retaining the "form" tags make any sense? Does it harm/help anything to retain/remove the "form" tags?

推荐答案

没有必要保留表格标记。您可以将所有内容放在div中和时钟事件上您可以检索数据并将其发送到服务器。



但是使用表单元素有其自身的优点。它让生活更轻松。使用 new Form()对象更容易收集表单数据。如果您不想提交表单,您仍然可以通过阻止表单提交的默认行为来实现。



No there is no point keeping the form tag in the HTML if you are not using the Form Submit. You can put everything inside a div and on the clock event You can retrieve the data and send it to the server.

But using form elements have their own advantages. It makes life a lot more easier. Its easier to collect the form data with new Form() object. If you dont want to submit the form you still can do that by preventing the default behavior of Form Submit.


' form')。submit( function (e){
e.preventDefault(); // 这可以防止提交表单
// 然后你的代码就在这里......
// .......................................

});
('form').submit(function(e){ e.preventDefault(); //This prevents the form being submitted //Then your code goes here... // ....................................... });





希望这有帮助。



Hope this helps.


这篇关于使用HTML按钮而不是输入类型使用Form元素提交排除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆