触发器隐藏按钮不起作用

Trigger hidden Button doesnt work

本文关键字:不起作用 按钮 隐藏 触发器      更新时间:2023-09-26

我正在尝试触发一个隐藏按钮。不幸的是,该事件不起作用。这是代码:

<div class="dyn-inp-group">
  <div class="dyn-inps">
          <div class="form-group form-group-options col-xs-12 dyn-inp">
            <div class="col-md-6" style="padding: 0 10px 0 0;">
              <input class="form-control" class="customerfirstName" name="customer[firstName][]" type="text" placeholder="First name" required="required">
            </div>
            <div class="entry input-group col-md-6" style="margin: 0;">
              <input class="form-control" name="customer[lastName][]" type="text" placeholder="Last name" required="required">
              <span class="input-group-btn hidden">
                <button id="remove"  class="btn btn-info btn-remove" type="button">
                  <span class="glyphicon glyphicon-minus"></span>
                </button>
              </span>
            </div>
          </div>
        </div>
<div class="form-group form-group-options col-xs-12 dyn-btn">
          <div class="input-group col-xs-12">
            <button id="countInput" class="btn btn-default" type="button">
              <span class="glyphicon glyphicon-plus"></span> Add  more
            </button>
          </div>
        </div>
          </div>

当你点击"添加按钮"时,我正在计算所有输入。

 var customerCount= 1 ;
$( "#countInput" ).click(function() {
 var e = document.getElementsByTagName('input');
 var i ;
var s = 1 ;
for(i=0; i < e.length; i++) {
if(e[i].type== "text" && e[i].name=="customer[firstName][]" ) { s++  ; }}
 customerCount=s;
});

我还有一个删除按钮。删除按钮不起作用。当我点击删除按钮时,我想再次对输入字段进行计数。

$( "#countInput" ).click(function() { This does not work

有什么想法吗?

我看不出有任何问题。两个按钮的点击都很好

$( "#countInput" ).click(function()
$( "#remove" ).click(function()

请在此处查看:https://jsfiddle.net/ucnh4z4q/1/

由于您已经在使用JQuery,您可以使用.ech(),而不是将纯js与JQuery混合,定义许多无用的变量

var customerCount= 1 ;
$('#countInput').click(function() {
    $('input').each(function(item) {
    var newCustomer;
    if($(this).attr('name') === 'customer[firstName][]')
        customerCount+=1;
  });
    console.log(customerCount);
});
$('#remove').click(function() {
    customerCount-=1;
  console.log(customerCount);
});

这里是一个正在工作的jsfiddle