防止表单提交上的jquery移动刷新

时间:2013-11-11 20:33:43

标签: javascript jquery html forms

我正在尝试使用此表单在提交时执行此功能但由于某种原因,它会弹出警报,但随后会刷新整个页面。我尝试过其他在线解决方案,例如:Prevent form redirect OR refresh on submit?

但那没用。

function addFood() {
    alert("123");
    return false;
}

<form onsubmit="return addFood()" data-ajax="false">
    <input type="text" name="" id="name" value="" placeholder="name" data-clear-btn="true"/>
    <input type="text" name="" id="brand" value="" placeholder="brand" data-clear-btn="true"/>
    <input type="text" name="" id="extra" value="" placeholder="extra" data-clear-btn="true"/>
    <input type="number" name="" id="amount" value="" placeholder="amount" data-clear-btn="true"/>
    <input type="number" name="" id="calories" value="" placeholder="calories" data-clear-btn="true"/>
    <input type="number" name="" id="fat" value="" placeholder="fat" data-clear-btn="true"/>
    <input type="number" name="" id="carbohydrate" value="" placeholder="carbohydrate" data-clear-btn="true"/>
    <input type="number" name="" id="fibre" value="" placeholder="fibre" data-clear-btn="true"/>
    <input type="number" name="" id="sugar" value="" placeholder="sugar" data-clear-btn="true"/>
    <input type="number" name="" id="protein" value="" placeholder="protein" data-clear-btn="true"/>
    <input type="submit" value="submit"/>
<form>

1 个答案:

答案 0 :(得分:0)

删除onsubmit并将<form>的{​​{1}}属性设置为action(我还会添加#

id

如果您在提交<form id="myForm" action="#" data-ajax="false">时需要执行某些操作,请使用:

<form>