我有以下代码:
HTML:
<button id="my_button">Test</button>
Javascript(jQuery):
$(document).ready(function () {
$('#my_button').click(function () {
$(this).wrap('<form>')
})
})
当点击按钮时,它会按照我的意愿用表单包装它,但也会提交表单。
我只想用表单包装而不提交它
最佳答案
或者添加对 preventDefault
的调用:
$(document).ready(function () {
$('#my_button').click(function(e) {
// Accept the arg -------------^
e.preventDefault(); // Prevent the default action
$(this).wrap('<form>')
});
});
...或者将按钮的类型更改为button
:
<button type="button" id="my_button">Test</button>
...因为 button
的默认 type
是 submit
。
关于javascript - jQuery用表单包装div而不提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855385/