【问题标题】:submit input text into table as <td>将输入文本作为 <td> 提交到表中
【发布时间】:2018-08-12 05:02:24
【问题描述】:

我已经在网上寻找了很长时间,但我一直无法找到一个看起来很常见的问题的答案。

基本上我想要做的是提交输入文本,也许还有如果选中一个单选按钮或类似的东西以作为表格中的文本弹出,因为这里是输入和表格的一些照片。

我希望能够按下“+”或“添加”按钮,并将输入中的所有文本添加到表格中,并且如果选中了收音机(checked=true(类似的东西))当我按下添加按钮时显示在表格中。

这是示例照片的 HTML:

    <div class="bg">

    <button id="droplist-btn" class="droplist-btn">Add By Droplist</button> 
<form>
    <input type="text" id="item-code-input" class="item-code-input" placeholder=""></input>
    <button type="submit" id="add-item" class="add-item">+</button>
    <input type="text" id="color-input" placeholder="color"></input>
    <select type="text" id="sizes-item-code" class="sizes-item-code">
        <option value="Small">Small</option>
        <option value="Medium">Medium</option>
        <option value="Large">Large</option>
        <option value="XLarge">XLarge</option>
        <option value="One Size">One Size</option>
    </select>
    </label>
    <label class="switch">
    <input type="checkbox">
    <span class="slider"></span>
    </label>
    <label for="switch">Any Color</label>
    </form>
    <table id="items-table">
        <!--<caption>Task</caption>-->
        <tr>
            <th>Item</th>
            <th>Size</th>
            <th>Options</th>
        </tr>
        <tr>

        </tr>
    </table>
    <label id="item-code-label">Item Code</label>
    <!-- idk what happened here -->
    <label class="radio">.
    <input type="radio" checked="checked" name="radio">
    <span class="check-item"></span>
</label>
<label id="keyword-label">Keyword</label>
<label class="radio">.
<input type="radio" name="radio">
<span class="check-keyword"></span>
</label>

 </div>
<script src="jquery.js"></script>
<script src="items.js"></script>

</body>
</html> 

我已尝试将以下两个答案都添加到我的其他脚本中,但它们都没有在这里工作是我不同表的 html:

<table id="proxy-list">
        <tr>
          <!--<th>Name</th> -->
          <th colspan="3">Proxies</th>
        </tr>
        <tr>
          <td>Proxy1:</td>
          <td>174.32.116.214:87</td>
          <td>
            <button class="remove-btn"><div class="thex">x</div></button>
          </td>
        </tr>
      </table>
      <input type="text" id="proxy-add-name" placeholder="Name"></input>
      <input type="text" id="proxy-add-proxy" placeholder="Proxy"></input>
      <button id="proxy-add" onclick="addProxy()">Add</button>

感谢您的任何帮助。

(我知道我的 html 代码很乱,我对 html 非常了解并且还在学习)

(jQuery 是首选,但正如我所说的那样有帮助)

【问题讨论】:

  • 你能贴一些你试过的代码吗?
  • $('#items-table').append($('') var Item = $("#item-code-input").val() .append($ ('').append(Item)) .append($(' ').append("Large")) .append($(' ').append("任意大小" )))
  • 类似的东西作为没有变量的测试它不起作用@FrankerZ
  • edit您的帖子包含您对问题的任何其他信息。避免在 cmets 中添加它,因为它们更难阅读并且更容易删除。帖子的编辑按钮就在帖子标签的下方。
  • 也发布一些 HTML,这样我们就可以演示/测试它而无需重新制作示例结构。

标签: javascript jquery html css jquery-ui


【解决方案1】:

只需更新 addRow 的开头

function addRow() {
	var item_text = 1; // $('#item-code-input').val() ???
	var size_text = 2;
	var options_text = 3;
	$('#main-table').append('<tr>'
		+'<td>'+item_text+'</td>'
		+'<td>'+size_text+'</td>'
		+'<td>'+options_text+'</td>'
		+'</tr>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="main-table">
<tr>
	<th>Item</th><th>Size</th><th>Options</th>
</tr>
</table>
<button onclick="addRow()">Add Row</button>

【讨论】:

  • 如果我想让变量成为输入字段内部的文本,我会做 .val(); ?
  • 是的,尽管在“选择”中获取所选选项可能会更棘手(但在您的情况下,您将选项中的值和文本/标签设为相同)
  • 我已经在旧 html 下方发布的新 html 上尝试了您的脚本,但它不起作用
【解决方案2】:

这是使用您的代码 - 顺便说一句,我为复选框输入添加了 id="switch"(以链接到 for="switch")对于选项它只考虑“任何颜色”。 “+”按钮使其添加行。单击 + 按钮时,我使用“return false”来阻止表单提交。

function addRow() {
	var item_text = $('#item-code-input').val();
	var size_text = $('#sizes-item-code').val();
	var options_text = $('#switch').is(':checked') ? 'Any Color' : '';
	$('#items-table').append('<tr>'
		+'<td>'+item_text+'</td>'
		+'<td>'+size_text+'</td>'
		+'<td>'+options_text+'</td>'
		+'</tr>');
}

$(function(){
  $('#add-item').click(function(){
    addRow();
    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bg">

    <button id="droplist-btn" class="droplist-btn">Add By Droplist</button> 
<form>
    <input type="text" id="item-code-input" class="item-code-input" placeholder=""></input>
    <button type="submit" id="add-item" class="add-item">+</button>
    <input type="text" id="color-input" placeholder="color"></input>
    <select type="text" id="sizes-item-code" class="sizes-item-code">
        <option value="Small">Small</option>
        <option value="Medium">Medium</option>
        <option value="Large">Large</option>
        <option value="XLarge">XLarge</option>
        <option value="One Size">One Size</option>
    </select>
    </label>
    <label class="switch">
    <input type="checkbox" id="switch">
    <span class="slider"></span>
    </label>
    <label for="switch">Any Color</label>
    </form>
    <table id="items-table">
        <!--<caption>Task</caption>-->
        <tr>
            <th>Item</th>
            <th>Size</th>
            <th>Options</th>
        </tr>
        <tr>

        </tr>
    </table>
    <label id="item-code-label">Item Code</label>
    <!-- idk what happened here -->
    <label class="radio">.
    <input type="radio" checked="checked" name="radio">
    <span class="check-item"></span>
</label>
<label id="keyword-label">Keyword</label>
<label class="radio">.
<input type="radio" name="radio">
<span class="check-keyword"></span>
</label>

 </div>
<script src="jquery.js"></script>
<script src="items.js"></script>

</body>
</html>

【讨论】:

    【解决方案3】:

    试试这个。效率不高,但它涵盖了您想要做的事情

    function add() {
        let table = document.getElementById("theTable")
        let fd = new FormData(myForm)
       
        let output = ""
        for(let data of fd.entries()) {
          output += "<td>"+data[1]+"</td>"
        }
        output = "<tr>"+output+"</tr>"
       
        table.innerHTML += output
    }
    <form id=myForm>
      <input name="name" value="John" />
      <input name="age" value="32" />
      <input name="lang" value="en" />
      <input type=button onclick="add()" value="+"/>
    </form>
    
    <table width="100%" border=1 id="theTable">
      
    </table>

    【讨论】:

    • 我试过你的脚本,当我按下添加按钮时,文本从输入中消失并且没有出现在任何地方
    • 这表明 formId 有问题。您可以分享您的代码以获得更多帮助吗?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签