设置输入vield的值而不显示它

时间:2017-06-05 11:20:03

标签: javascript jquery html

当我点击跨度删除地址时,它会将其从ui中删除,但不会从列表中删除,removeItem的打印值为空字符串""。 以及如何将stringList传递给id value=""的输入clientAdd,而不在输入中显示。



var stringList = [];
$("#clientAdd").keypress(function(e) {
  if (e.which === 13) {
    $(".target").append("<a href='#' class='tag'>" + "<span class='removeAddress'>" + '+' + "</span>" + this.value + "</a>");
    stringList.push(this.value);
    this.value = "";
    console.log(stringList);
    $(document).on("click", ".removeAddress", function() {
      var removeItem = $(this).parent().parent().val();
      console.log(removeItem);
      stringList.splice($.inArray(removeItem, stringList), 1);
      $(this).parent().remove();

      console.log(stringList);
    });
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="form-group">
    <div class="col-md-12 col-sm-12">
      <label>Address *</label>
      <div class="target"></div>
      <input type="text" id="clientAdd" name="address" value="" class="form-control required">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

试试这个

  1. 其父文字不是var results = Regex.Matches(s, @"\[([^][]+)]") .Cast<Match>() .Select(m => m.Groups[1].Value) .ToList(); 。所以你需要使用parent of parent获取父使用的文本值。它只会获得父文本。end() documentation
  2. 删除点击的元素后,您将使用剩余的$(this).parent().clone().children().remove().end().text()通过地图功能
  3. 创建数组

    removeddress
    var stringList = [];
    $("#clientAdd").keypress(function(e) {
      if (e.which === 13) {
        $(".target").append("<a href='#' class='tag'>" + "<span class='removeAddress'>" + '+' + "</span>" + this.value + "</a><br>");
        stringList.push(this.value);
        this.value = "";
        console.log(stringList);
        $(document).on("click", ".removeAddress", function() {
          var removeItem = $(this).parent().clone().children().remove().end().text();
          console.log(removeItem);
          stringList = $('.removeAddress').map(function(){
            return $(this).parent().clone().children().remove().end().text()
          }).get()
          $(this).parent().remove();
          console.log(stringList);
        });
      }
    });

答案 1 :(得分:0)

您可以隐藏整个输入,也可以使用input type =“hidden”。通常,您为人类显示一个输入作为事件触发器,并为服务器端处理的值隐藏输入

<div class="row">
          <div class="form-group">
            <div class="col-md-12 col-sm-12">
              <label>Address *</label>
              <div class="target"></div>
              <input type="text" id="clientAdd" name="address" value="" class="form-control required">
              <input type="hidden" id="clientAddCode" name="addressCode">
            </div>
          </div>
</div>


var stringList = [];
$("#clientAdd").keypress(function (e) {
if (e.which === 13) {
     $(".target").append("<a href='#' class='tag'>" +"<span class='removeAddress'>"+'+'+"</span>"+ this.value + "</a>");
        stringList.push(this.value);
        this.value = "";
        console.log(stringList);
        $("#clientAddCode").val(stringList);
        $(document).on("click", ".removeAddress", function() {
            var removeItem = $(this).parent().parent().val();
            console.log(removeItem);
            stringList.splice( $.inArray(removeItem, stringList ) ,1 );
            $(this).parent().remove();

            console.log(stringList);
            $("#clientAddCode").val(stringList);
        });
}
});