javascript - 对齐 &lt;input type ="text"> 和 <select></select>

标签 javascript html css

我正在尝试对齐我使用 javascript 动态添加到表单中的选择框和文本框,但我无法让它们正确对齐。

CSS 代码:

select
{
    margin-top: 3px;
    margin-left: 5px;
    margin-bottom: 2px;
    padding-left:6px;
    padding-top: 3px;
    width: 122px;
    height: 30px;
    font-family: Verdana, sans-serif;   
    font-size:13px; 
}

input[type="text"]
{
    width: 122px;
    height: 30px;
    padding-left:7px;
    margin: 5px;
}

HTML 代码:

<form method="post" action="" id="myForm">
    <input type="text" name="headline" placeholder="Headline" value="<?=$_POST['Headline']?>" autofocus required>
    <textarea></textarea>
    <select>
        <option value='1'>option1</option>
    </select>
    <br>
    <input name="Create" id="Create" type="submit" value="Create">
</form>

我在其中添加额外输入字段的 Javascript 代码:

function AddCatchInput($id)
{
var container = document.getElementById("myForm");
var submitbutton = document.getElementById("Create");

var select = document.createElement("select");
select.setAttribute("name", "Select"+$id);
select.setAttribute("id", "Select"+$id);
select.classList.add('FishSelect');
var option;

option = document.createElement("option");
option.setAttribute("value", "1");
option.innerHTML = "name";
select.appendChild(option);

// Create an <input> element, set its type and name attributes
var input = document.createElement("input");
input.type = "text";
input.name = "Weight" + $id;
input.placeholder = "Weight";
input.classList.add('FishWeight');



container.removeChild(submitbutton);
container.appendChild(select);

container.appendChild(input);
container.appendChild(submitbutton);
}

我最终得到的是:

http://i.stack.imgur.com/IHagL.jpg

非常感谢任何帮助。

最佳答案

尝试将 vertical-align:top; 添加到这两个元素。

关于javascript - 对齐 &lt;input type ="text"> 和 <select></select>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143914/

相关文章:

html - 如何在表格中分组 <TR> 以进行分页?

CSS - 旋转 div -90deg - 齐平

javascript - Firebase/AngularFire - 是否需要身份验证、Login.Logoff?

javascript - bxslider - 幻灯片重叠 - 在 1 帧中显示两张幻灯片

html - 图像下的右对齐文本(无 javascript)

javascript - 如何使箭头键跳过禁用类的列表项?

javascript - 在ember simple auth中将数据保存在浏览器的本地存储中

javascript - 编辑 window.location.hash 将导致删除查询字符串

javascript - 在刷新和/或计时器时在 div 之间切换内容

html - CSS 样式表未链接到我的 html 页面