2017-08-24 60 views
0

我創建的HTML元素動態的div一個文本框的值,我需要得到值文本框 這是我的動態創建的內容,現在我需要得到如何從動態創建的div

<div id="TextBoxContainer"> 
    <div id="newtextbox1"> // this id is dynamic id 
    <div class="row cells2"> 
     <div class="cell"> 
     <label>Patient Name</label>   
     <div class="input-control text full-size"> 
      <input type="text" id="PatientName1" placeholder="Patient Name"/> // this id is dynamic id 
     </div> 
     </div> 
     <div class="cell"> 
     <label>Patient ICNo</label> 
     <div class="input-control text full-size" > 
      <input type="text" id="PatientICNo" placeholder="Patient ICNo"/> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

這裏我試圖在jQuery的

if ($("#TextBoxContainer") != null && $("#TextBoxContainer").length > 0) { 
    var count = 1; 
    $("#TextBoxContainer").each(function() { 
     debugger; 
     var Pid = "input#PatientName" + count; 
     var childdiv = "div#newtextbox" + count; 
     count++; 
     var patientname = $(this).closest(childdiv).children(Pid).val();   

    }); 
} 
+2

您可以使用'var patientname = $(Pid).val();' – Satpal

+1

'''最近的''在您用於孩子時通過父母。刪除'.closest(childdiv)'可能會工作 – adiga

+1

不要使用'id'屬性(特別是那樣)。使用類名和相對選擇器。 - '$(「#TextBoxContainer」)。each(function(){var patientname = $(this).find('。myNameTextBoxClass')。val();});' –

回答

1

在這裏,你有一個解決方案https://jsfiddle.net/p9ywL4pm/1/

if ($("#TextBoxContainer") != null && $("#TextBoxContainer").length > 0) { 
 
    var count = 1; 
 
    $("#TextBoxContainer").children().each(function() { 
 
     var Pid = "input#PatientName" + count; 
 
     var patientname = $(this).find(Pid).val();   
 
     console.log(Pid, patientname); 
 
     count++; 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="TextBoxContainer"> 
 
    <div id="newtextbox1"> 
 
    <div class="row cells2"> 
 
     <div class="cell"> 
 
     <label>Patient Name</label>   
 
     <div class="input-control text full-size"> 
 
      <input type="text" id="PatientName1" placeholder="Patient Name" /> 
 
     </div> 
 
     </div> 
 
     <div class="cell"> 
 
     <label>Patient ICNo</label> 
 
     <div class="input-control text full-size" > 
 
      <input type="text" id="PatientICNo" placeholder="Patient ICNo"/> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <div id="newtextbox2"> 
 
    <div class="row cells2"> 
 
     <div class="cell"> 
 
     <label>Patient Name</label>   
 
     <div class="input-control text full-size"> 
 
      <input type="text" id="PatientName2" placeholder="Patient Name"/> 
 
     </div> 
 
     </div> 
 
     <div class="cell"> 
 
     <label>Patient ICNo</label> 
 
     <div class="input-control text full-size" > 
 
      <input type="text" id="PatientICNo" placeholder="Patient ICNo"/> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

去,我認爲裏面#TextBoxContainer容器中的兩個子元素。

更改#PatientICNo輸入

<input type="text" class="PatientICNo" placeholder="Patient ICNo"/> 

使用class,而不是ID因爲ID需要獨特。

希望這會幫助你。

0
var arrOfValue =[]; 
$('.input-control text full-size [type="text"]').each(function() { arrOfValue .push($(this).val())}) 

的arrOfValue將所有的文本,使用索引來獲取值中獲取價值。

+0

bro動態所有值我需要推入數組 –

+1

是的,上面的代碼將選擇輸入控制文本全尺寸類中的所有輸入,並循環以獲取所有文本值。我正在使用類選擇器和attritbute類型文本。 –