2016-11-04 61 views
0

我無法使用JavaScript和jQuery動態創建多個文本字段。我在下面解釋我的代碼。無法創建多個文本框使用JavaScript和JQuery的不同ID使用

<div class="col-md-3"> 
    <div class="form-group"> 
     <label for="ques">No of questions</label> 
     <input name="no_of_question" id="ques" class="form-control" placeholder="no of question" type="text"> 
    </div> 
    </div> 
    <div class="row"> 
    <div class="col-md-8"> 
     <div class="form-group"> 
     <label>Questions</label> 
     <input type="button" style="line-height:13px; margin-right:2px;" class="btn btn-success btn-sm" name="plus" id="plus" value="+" onClick="addQuestionField();"> 
     <input type="button" style="line-height:13px; margin-right:2px;" class="btn btn-danger btn-sm" name="minus" id="minus" value="-" onClick="deleteQuestionField();"> 
     </div> 
     <div class="text-left" id="intro-add" style="display:none"> 
     <input type="button" name="" class="btn btn-sm btn-info" value="Add"> 
     </div> 
    </div> 

    </div> 
    <div id="container"> 
    <div class="form-group" style="margin-top:5px;display:block" id="introBox0" > 
     <div> 
     <input type="text" style="width: 77.5%;float:left; margin-bottom:5px; margin-right:5px;" class="form-control inptbox0" id="scaleans00" name="labelname" placeholder="Answer" value=""> 
     <input type="button" style="line-height:13px; margin-right:2px; margin-top:5px; float:left;" class="btn btn-danger btn-sm" name="labelminus" id="labelminus" value="-"> 
     <div class="text-left" id="intro-add" style="display:block"> 
      <input type="button" name="" class="btn btn-sm btn-info" value="Add" onclick="addMore(0,0)"> 
     </div> 
     </div> 
    </div> 
    <hr> 
    </div> 

這裏用戶創建多個問題部分和每個問題部分用戶使用add按鈕來創建多個領域,將刪除使用minus按鈕。每個問題字段應該唯一的id。假設第一部分有3個輸入字段,那麼其ids應該是這樣的scaleans00,scaleans01,scaleans02...,第二部分應該是scaleans10,scaleans11,scaleans12...等等。我正在解釋下面的JavaScript代碼。

<script> 
    function addQuestionField() { 
     var get = $("#ques").val(); 
     for (var i = 1; i < get; i++) { 
     $('#container').append('<div class="form-group dyn" style="margin-top:5px;display:block" id="introBox'+i+'"><div><input type="text" style="width: 77.5%;float:left; margin-bottom:5px; margin-right:5px;" class="form-control inptbox'+i+'" id="scaleans'+i+'0" name="labelname" placeholder="Answer" value=""><input type="button" style="line-height:13px; margin-right:2px; margin-top:5px; float:left;" class="btn btn-danger btn-sm" name="labelminus" id="labelminus" value="-"><div class="text-left" id="intro-add" style="display:block"><input type="button" name="" class="btn btn-sm btn-info" value="Add" onclick="addMore('+i+',0)"></div></div></div><hr>'); 
     } 
    } 

    function deleteQuestionField() { 
     var textareas = $('#container .dyn'); 
     if (textareas.length !== 0) { 
     textareas.last().remove(); 
     $('#ques').val(textareas.length - 1); 
     } 
    } 
     function addMore(parent,child){ 
      var mainDiv='#introBox'+parent; 
      $(mainDiv).append('<div><input type="text" style="width: 77.5%;float:left; margin-bottom:5px; margin-right:5px;" class="form-control inptbox'+parent+'" id="scaleans" name="labelname" placeholder="Answer" value="">'+'<input type="button" style="line-height:13px; margin-right:2px; margin-top:5px; float:left;" class="btn btn-danger btn-sm" name="labelminus" id="labelminus" value="-"></div>'); 
      repopulate(parent); 
     } 
     function repopulate(k){ 
      var mainId='.inptbox'+k; 
      var j=0; 
      $(mainId).each(function(i, e) { 
       if(i==0){ 
        $(this).attr('id', 'scaleans' + i+j); 
        $(this).next().attr('onClick', function(old, n) { 
         return 'removeThis(' + i + ','+j+')'; 
        }) 
        $(this).next().attr('id', function(old, n) { 
        return 'labelminus' + i+j; 
       }); 
       }else{ 
        $(this).attr('id', 'scaleans' + i+(j+1)); 
        $(this).next().attr('onClick', function(old, n) { 
         return 'removeThis(' + i + ','+(j+1)+')'; 
        }) 
        $(this).next().attr('id', function(old, n) { 
        return 'labelminus' + i+(j+1); 
       }); 
       } 
      }) 
     } 
     function removeThis(r,t) { 
      $("#scaleans" + r+t).remove(); 
      $("#labelminus" + r+t).remove(); 
      repopulate(); 
    } 
    </script> 

在這裏,我不能創建按我的要求。 Here is my full code。請幫幫我。

+0

你的問題是你沒有得到每個問題的唯一ID是嗎?或者還有您的添加和刪除字段無法正常工作? – Logeshwaran

+0

@Logeshwaran:是的,這就對了。 id應該按照我的解釋。 – satya

+0

是的,每個部分的第一個字段不應該包含任何'-'按鈕。 – satya

回答

0

我會向你解釋正在發生的事情與你已經把Plunker

function addQuestionField() { 
    var get = $("#ques").val(); 
     for (var i = 1; i < get; i++) { 

在方法addQuestionFiled()當前的代碼,檢索到的GET變量的值不truthy,如果你是不輸入任何值到

一些問題

文本框。因此,html不會被追加

填寫問題的數量爲2,然後單擊添加它將繼續將html附加到容器div,因爲'var get'將具有值2和'var i'是小於'var get'