简体   繁体   English

HTML:如何在div包装中创建一系列复选框到下一行?

[英]HTML: How can I make a series of checkboxes within a div wrap to the next line?

添加新标签

So I have a database of cooking recipes, and I'm making a web form which will eventually connect to the database to add entries to it. 所以我有一个烹饪食谱数据库,我正在制作一个网页表格,最终将连接到数据库以添加条目。 One column in the database is "tags". 数据库中的一列是“标签”。 The web form will show all the tags that have been used before (with checkboxes), and give the user the option to add a new tag which will appear as a new checkbox next to the existing ones. Web表单将显示之前使用过的所有标签(带有复选框),并为用户提供添加新标签的选项,该标签将显示为现有标签旁边的新复选框。

PROBLEM #1: Right now the tags are separated by 1 space, but I'd like them to be separated by a larger gap so that it looks better. 问题#1:现在标签被1个空格隔开,但我希望它们被更大的间隙分开,这样它看起来更好。 What is the best way to do this? 做这个的最好方式是什么?

PROBLEM #2: As more tags are added, they will eventually reach the right edge of this fieldset. 问题#2:随着更多标签的添加,它们最终会到达此字段集的右边缘。 What is the best way to make them wrap to the next line and ensure that the space(s) between the new tag and the previous one are ignored? 使它们换行到下一行并确保新标记与前一个标记之间的空间被忽略的最佳方法是什么? (Otherwise the new tag might be indented on the next line which I do not want!) (否则新标签可能会在我不想要的下一行缩进!)

THE HTML CODE: HTML代码:

<p>
    <fieldset>
        <legend>Tags:</legend>
        <span class="tags">
            <input type="checkbox" name="tags" value="Dessert">Dessert  <input type="checkbox" name="tags" value="Fast">Fast  <input type="checkbox" name="tags" value="Entree">Entree
        </span>
        <p>
            <input type="text" id="addtag"><input type="submit" value="Add">
        </p>
    </fieldset>
</p>

THE JAVASCRIPT: JAVASCRIPT:

$("input[value='Add']").click(function(event){
    event.preventDefault();
    tagAdded = document.getElementById('addtag').value;
    if (tagAdded!=null && tagAdded.trim()!="") {
        //$('.tags').append('????????');
    }
 });

Thank you! 谢谢!

Firstly, your tags (like "Dessert") should be in a <label> tag so: 首先,您的标签(如“甜点”)应位于<label>标签中,以便:

<input type="checkbox" name="tags" value="Dessert" id="tag-1"><label for="tag-1">Dessert</label>

If you click on the word now, it will activate the checkbox in front of it. 如果您单击现在的单词,它将激活前面的复选框。

Then to make a bigger gap: use CSS: 然后做一个更大的差距:使用CSS:

label{
    margin-right: 2em;
}

this will give a gap that is the size of 2 letters m between your labels 这样会在标签之间产生2个字母m的间隙

您可以为标签中的每个复选框包装标签,并为标签添加右边距。

I guess this can solve problem 1 & 2... 我想这可以解决问题1和2 ......

wrap each tag like this: 像这样包装每个标签:

<div class="wrapper">
    <input type="checkbox" name="tags" value="Dessert">Dessert
</div>

css: CSS:

.wrapper { margin-right:10px; float:left; }

Modify the markup so that the checkbox block is like this (note that there are other things to fix, eg you are now trying to put fieldset inside p , which is invalid and won't work): 修改标记,使复选框块像这样(请注意,还有其他事情需要修复,例如,您现在尝试将fieldset放在p ,这是无效的,不起作用):

<div class="tags">
<label><input type="checkbox" ...>Dessert</label>
<label><input type="checkbox" ...>Fast</label>
...
</div>

Use CSS code like this: 使用这样的CSS代码:

.tags { margin-left: -4em; }
.tags label { padding-left: 4em;  white-space: nowrap; }

I'm using large spacing just to make things clearer; 我使用大间距只是为了让事情更清晰; 2em would probably suffice—just remember to make the negative left margin value match the padding value. 2em可能就足够了 - 只记得使负左边距值与填充值匹配。

Try putting your code this way 尝试以这种方式放置代码

Html: HTML:

<p>
    <fieldset>
        <legend>Tags:</legend>
        <span class="tags">
            <p> <input type="checkbox" name="tags" value="Dessert"> Dessert</p>
            <p> <input type="checkbox" name="tags" value="Fast"> Fast</p>
            <p> <input type="checkbox" name="tags" value="Entree"> Entree</p>
        </span>
        <div>
            <input type="text" id="addtag"><input type="submit" value="Add">
        </div>
    </fieldset>
</p>​

Javascript: 使用Javascript:

$("input[value='Add']").click(function(event){
    event.preventDefault();
    tagAdded = document.getElementById('addtag').value;
    if (tagAdded!=null && tagAdded.trim()!="") {
        $('.tags').append('<p> <input type="checkbox" name="tags" value="' + tagAdded.trim() + '" /> ' + tagAdded.trim() + '</p>');
    }
 });​

css: CSS:

div { clear:both; }
.tags p {
    float:left;
    margin-right:20px;    
}

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

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