使用 jquery append() 向表单添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16655997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using jquery append() to add a button to a form
提问by dot
I need to add a button to a form. I have no control over the logic that creates the form itself... but i can add code around the form, if need be.
我需要在表单中添加一个按钮。我无法控制创建表单本身的逻辑……但如果需要,我可以在表单周围添加代码。
The code for the form looks like this:
表单的代码如下所示:
<form action="/cgi-bin/a/logon" method="POST">
<DT>Login ID</DT>
<DD>
<input class=" text" type="text" name="id" value="">
</DD>
<DT>Password</DT>
<DD>
<input class=" password" type="password" name="password" value="">
</DD>
<DT></DT>
<DD><input class="submit" type="submit" name="submit" value="Logon">
</DD>
</FORM>
I know I can add a div around the entire form and append to that... but I would like the new button to appear right beside the "Logon" button. I've tried the following code, but it didn't work:
我知道我可以在整个表单周围添加一个 div 并将其附加到...但我希望新按钮出现在“登录”按钮旁边。我已经尝试了以下代码,但没有奏效:
<script type="text/javascript">
$(document).ready(function() {
$('.submit').append($('<input type="button" value="test">'));
});
</script>
I'm assuming you probably can't append to a button... which is why it didn't work.
我假设您可能无法附加到按钮......这就是它不起作用的原因。
I'm still learning jquery so if there's another method aside from append that I could use, I'm all ears! Actually, the button doesn't have to be a part of the form because I'm going to run client side code when it is clicked. But it does have to appear beside the login button.
我仍在学习 jquery,所以如果除了 append 之外还有另一种方法可以使用,我会全力以赴!实际上,按钮不必是表单的一部分,因为我将在单击它时运行客户端代码。但它确实必须出现在登录按钮旁边。
Thanks.
谢谢。
回答by Sushanth --
you cannot append anything to the inputelement ..
您不能向输入元素附加任何内容..
Use .after
or .before
使用.after
或.before
$('.submit').after($('<input type="button" value="test">'));
回答by Mohammad Adil
You can use .after
like this
您可以使用.after
这样的
$('.submit').after('<input type="button" value="test" />');
OR
或者
$('form').append('<input type="button" value="test" />');