gpt4 book ai didi

javascript - 为什么 jQuery 提交这个表单?

转载 作者:搜寻专家 更新时间:2023-11-01 04:40:41 25 4
gpt4 key购买 nike

我不明白为什么按下按钮 addremove,表单会自动提交。我希望能够通过按下上述按钮动态添加/删除文本输入,并且我的代码正在运行。但是一旦我添加了 form,我就得到了这种奇怪的自动提交行为......

这是 jQuery 脚本:

var counter = 3;
$("#add").click(function() {
counter = counter + 1;
$("#ingredienti").append('<input type="text" name="'+counter+'" class="form-control" placeholder="Inserisci ingrediente '+counter+' e quantit&agrave;" style="margin-bottom:.5em;">');
$("input[name='numero_ingredienti']").value(counter);
});
$("#remove").click(function() {
$("input[name="+counter+"]").remove();
counter = counter - 1;
if(counter<0){counter=0;};
$("input[name='numero_ingredienti']").value(counter);
});

这个表格:

<form action='salva_ricetta.php' method='post'>
<div class="row" style="margin-top:2em;margin-bottom:5em;">
<div class="col-sm-10 col-sm-offset-1">
<input type="text" name='nome' class="form-control" placeholder="Nome della ricetta" style="margin-bottom:1em;">
</div>
<div class="col-sm-3 col-sm-offset-1">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><span class='glyphicon glyphicon-star'></span> </div>
<select class="selectpicker" name='diff' data-width="8em">
<option selected disabled>Difficolt&agrave;</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon"><span class='glyphicon glyphicon-time'></span> </div>
<input type="text" name='durata' class="form-control" id="exampleInputAmount" placeholder="Durata (in minuti)">
</div>
</div>
</div>
<div class="col-sm-4">
<input type="text" class="form-control" name='foto' placeholder="Link ad una foto" style="margin-bottom:1em;">
</div>
<div class="col-sm-10 col-sm-offset-1">
<p><img src="img/grattuggia.png" style="max-height:3em;"> Lista ingredienti <span class="glyphicon glyphicon-info-sign" data-container="body" data-toggle="tooltip" data-placement="top" title="Inserisci anche i quantitativi in grammi (g), cucchiai o cucchiaini, tazze o quello che &egrave;!"></span>
<button class="btn btn-warning pull-right" id='add'><span class='glyphicon glyphicon-plus-sign'></span></button>
<button class="btn btn-warning pull-right" style="margin-right:.5em;" id='remove'><span class='glyphicon glyphicon-minus-sign'></span></button>
</p>
<div id='ingredienti'>
<input type="text" class="form-control" name='1' placeholder="Inserisci ingrediente 1 e quantit&agrave;" style="margin-bottom:.5em;">
<input type="text" class="form-control" name='2' placeholder="Inserisci ingrediente 2 e quantit&agrave;" style="margin-bottom:.5em;">
<input type="text" class="form-control" name='3' placeholder="Inserisci ingrediente 3 e quantit&agrave;" style="margin-bottom:.5em;">
</div>
</div>
<div class="col-sm-10 col-sm-offset-1">
<textarea class="form-control" name='procedimento' rows="10" placeholder="Procedimento"></textarea>
</div>
<div class="col-sm-10 col-sm-offset-1">
<input type="hidden" value='3' name='numero_ingredienti' />
<input type='submit' class="btn btn-warning pull-right" style="margin-top:.5em;" value='Salva ricetta'>
</div>
</div>
</form>

最佳答案

因为你在这里定义了action

<form action='salva_ricetta.php' method='post'>

所以什么时候触发/点击 <input type='submit'

它将提交 <form> .


解决方案

将此更改为

<input type='submit'

这个

<input type='button'

关于javascript - 为什么 jQuery 提交这个表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com