我使用bootstrap和jquery設計了一個用戶數據庫系統。問題是,當一個按鈕按下,它會在用戶的附加字段追加到重點。如何將動態添加的輸入字段關聯到父元素/ ID?
讓我們假設該字段的第一個數組的數組名是 後ref_title[0]
追加按鈕按下,另一個文本字段將顯示具有相同的屬性,但數組值將爲ref_title[1]
。
但是在代碼本身上,它只會顯示ref_title[]
。這沒關係,因爲任何新字段都會繼續添加到數組ref_title[]
。我對嗎?
onclick="newdata('<?php echo $row['drug_id']; ?>')"
'drug_id'
是一個唯一的數,例如1,2或3。
將數據定向到JS當我檢查的輸入字段下
第一盒是
<input class="form-control" id="ref_title3" name="ref_title[]" placeholder="Reference Title" type="text">
然而在第二盒(後追加按鈕按下並附加框出現)
<input class="form-control" id="ref_title" name="ref_title[]" placeholder="Reference Title" type="text">
看看id="ref_title"
。值3不回顯。
HTML:
<input type="text" class="form-control" id="ref_title<?php echo $row['drug_id'];?>" name="ref_title[]" placeholder="Reference Title">
<button type="button" onclick="newdata('<?php echo $row['drug_id']; ?>')" class="btn btn-primary" data-dismiss="modal">Save changes</button>
JS:
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" class="form-control" id="ref_title<?php echo $row['drug_id'];?>" name="ref_title[]" placeholder="Reference Title"/></div>"><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
點擊數
function newdata(str){
var ref = $('#ref_title'+str).val(); // !!Only can read the first array
var datas="&ref="+ref;
$.ajax({
type: "POST",
url: "update.php",
data: datas
}).done(function(data) {
$('#info').html(data);
viewdata();
});
};
我該如何讀取數組部分? var ref = $('#ref_title'+ str).val();? – john
在「PHP」下,你有一個文本可能是html輸入元素的一部分,但不是php。請詳細說明。 – VolkerK
確定其關於該HTML的sry – john