【问题标题】:Remove item that was added after a XMLHttpRequest happens删除 XMLHttpRequest 发生后添加的项目
【发布时间】:2013-06-06 03:38:16
【问题描述】:

我正在使用带有xmlhttp 的JavaScript 向我的表单添加另一个行项目。如果用户多次按下“添加订单项”按钮,我正在尝试弄清楚如何删除订单项,以便表单不会尝试从不必要的订单项中发布空值。

我不知道如何做到这一点,我认为它与 remove() 有关,但我不确定如何合并它。

这是我的 JavaScript 代码

<script type="text/javascript">
     var counter = 1;
     function addInput(div){
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
          var newdiv = document.createElement(div);
          var splitResponse = xmlhttp.responseText.split( "[BRK]" );
          var firstDropdownContent = splitResponse[0];
          var secondDropdownContent = splitResponse[1];
          newdiv.innerHTML = "<table><tr><td><img style='background:#ffffff; float:left; ' src='../../images/spacer_icon.png'>Item " + (++counter) + "</td><td>Quantity</td><td>Description</td><td>Amount</td><td><img style='background:#ffffff; float:left; ' src='../../images/spacer_icon.png'>Tax Rate</td></tr><tr><td width='190'><select name='item[]'><option value='' selected></option>" + (firstDropdownContent) + "</select></td><td width='90'><input name='quantity[]' type='text' size='5' /></td><td width='440'><input name='description[]' type='text' size='60' /></td><td width='120'><input name='amount[]' type='text' size='6' /></td><td><select name='taxid[]'><option value='' selected></option>" + (secondDropdownContent) + "</select></td></tr></table><br />";
        }
        document.getElementById(div).appendChild(newdiv);
     }

     xmlhttp.open("GET", "invoicedropdownquery.php", false);
     xmlhttp.send();

   }
</script>

这是我的按钮

<input type="button" value="Add Line Item" onClick="addInput('dynamicInput');">

然后我只是用一个 div 来放置它。

<div id="dynamicInput"></div>

如果您能提供帮助,请提前致谢。我可能会在每个订单项旁边放一个 gif 来删除它。

编辑:这是来自 javascript 内部的 html,更容易阅读。

<table><tr>
<td><img style='background:#ffffff; float:left; ' src='../../images/spacer_icon.png'>Item " + (++counter) + "</td>
<td>Quantity</td>
<td>Description</td>
<td>Amount</td>
<td><img style='background:#ffffff; float:left; ' src='../../images/spacer_icon.png'>Tax Rate</td></tr>
<tr><td width='190'><select name='item[]'><option value='' selected></option>" + (firstDropdownContent) + "</select></td>
<td width='90'><input name='quantity[]' type='text' size='5' /></td>
<td width='440'><input name='description[]' type='text' size='60' /></td>
<td width='120'><input name='amount[]' type='text' size='6' /></td>
<td><select name='taxid[]'><option value='' selected></option>" + (secondDropdownContent) + "</select></td></tr></table><br />

【问题讨论】:

  • 你为什么不用$.ajax
  • 那个 JavaScript 代码根本没有使用 jQuery。
  • 我在术语上的错误,你能帮忙吗?
  • 我们可以重新开始吗,这整个线程正在变成一个 jquery 参数。对于我在术语上的错误,我深表歉意。
  • 为什么将document.getElementById(div).appendChild(newdiv); 放在if 之外?尝试放入您的 if 声明中。

标签: javascript forms


【解决方案1】:

如果我理解你的问题,你想为每一行添加一个删除按钮。

基本上为此需要为添加的表设置一个动态id和一个按钮,该按钮将调用该函数以删除通过参数表id传递的行。

删除动态div的一项功能:

function removeItem(itemId){
    document.getElementById('dynamicInput').removeChild(document.getElementById(itemId));
}

这是您更新后的代码,没有 ajax 请求来创建和删除行:

    <script>
      var counter = 0;
      function add(div){
          var newdiv = document.createElement('div');
          var firstDropdownContent = "first";
          var secondDropdownContent = "second";
          newdiv.id = "row"+(++counter);
          newdiv.innerHTML = "<table ><tr><td><img style='background:#ffffff; float:left;' src='../../images/spacer_icon.png'>Item " + (counter) + "</td><td>Quantity</td><td>Description</td><td>Amount</td><td><img style='background:#ffffff; float:left; ' src='../../images/spacer_icon.png'>Tax Rate</td></tr><tr><td width='190'><select name='item[]'><option value='' selected></option>" + (firstDropdownContent) + "</select></td><td width='90'><input name='quantity[]' type='text' size='5' /></td><td width='440'><input name='description[]' type='text' size='60' /></td><td width='120'><input name='amount[]' type='text' size='6' /></td><td><select name='taxid[]'><option value='' selected></option>" + (secondDropdownContent) + "</select></td><td><a href='javascript: removeItem(\"row"+counter+"\")'>Remove</a></td></tr></table>";
          document.getElementById(div).appendChild(newdiv);
        }

        function removeItem(itemId){
            document.getElementById('dynamicInput').removeChild(document.getElementById(itemId));
        }

  </script>
  <button onclick="add('dynamicInput')">Add</button>
  <div id="dynamicInput"></div>

只需将其调整为您当前的代码即可。

【讨论】:

  • 您好,当我单击按钮时,它实际上是发布而不是删除项目。该按钮的作用与我的提交按钮相同。你能修改它以使用链接吗?
猜你喜欢
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2014-05-05
  • 2014-08-25
  • 2015-09-18
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多