2016-07-27 88 views
0

我想觸發一個隱藏的按鈕。不幸的是,這個事件不起作用。下面是代碼:觸發隱藏按鈕不工作

<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 

任何想法?

+2

你可能使用你的刪除按鈕錯誤選擇?它應該是'$(「#remove」).click(函數(){'? – Hodrobond

+0

我們可以看到你的css嗎?還有,你有這個jsfiddle嗎? – 4lackof

回答

0

我沒有看到任何問題。這兩個按鈕點擊這裏做工精細

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

檢查:https://jsfiddle.net/ucnh4z4q/1/

0

既然你已經使用JQuery,你可以使用。每(),而混合純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); 
}); 

Here a working jsfiddle