简体   繁体   English

使用jquery在标签内创建输入框

[英]create input box inside the lable using jquery

I am trying to create a label and placed outer the input check box, like 我正在尝试创建一个标签,并将输入复选框放在外面,就像

what I was trying this, My html is 我正在尝试这个,我的HTML是

<input type="checkbox">

My jquery is 我的jquery是

$('input[type="checkbox"]').after('<label class="label-select"><span></span></label>');

Now the output is 现在输出是

<input type="checkbox">
<label class="label-select">
<span></span>
</label>

But I want like this 但我想要这样

<label class="label-select">
<input type="checkbox">
<span></span>
</label>

So how may I write jquery for this. 那我怎么写这个jquery呢。 I dont like to change HTML. 我不想改变HTML。

Thanks for your answer. 感谢您的回答。

Use wrap() not after() : after()使用wrap() after()

$('input[type="checkbox"]').wrap('<label class="label-select"></label>').after('<span></span>');

Example fiddle 示例小提琴

你正在寻找.wrap() jQuery函数。

If you use jQuery.wrap() you should be able to achieve what you're looking for. 如果你使用jQuery.wrap(),你应该能够实现你正在寻找的东西。

$('input[type="checkbox"]').after('<span></span>').wrap('<label class="label-select"></label>');

EDIT Got the two back to front sorry, should be 编辑得到了两个回到对不起,应该是

$('input[type="checkbox"]').wrap('<label class="label-select"></label>').after('<span></span>'); 

您所要做的就是将元素(复选框)包装在标签内。

$('input[type="checkbox"]').wrap('<label class="label-select"><span></span></label>');

尝试这个:

$('input[type="checkbox"]').wrap('<label class="label-select"></label>').wrap('<span></span>');

Or you can use jquery append() method to append html inside a block. 或者您可以使用jquery append()方法在块中追加html。 Try this one, 试试这个,

$('.label-select').append('<input type="checkbox"><span></span>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM