0

我试图让这个表单在提交时执行这个功能,但由于某种原因,它会弹出警报,然后刷新整个页面。我尝试过其他在线解决方案,例如:防止表单重定向或提交时刷新?

但这没有用。

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>
4

1 回答 1

0

删除onsubmit并将您<form>action属性设置为#(我还会添加一个id

<form id="myForm" action="#" data-ajax="false">

如果您需要在<form>提交时发生某些事情,请使用:

$("#myForm").submit(function(){
    // do stuff that will fire off when my form is submitted
});
于 2013-11-11T20:40:30.023 回答