[英]hidden input value from php foreach loop not properly passing to JQuery
[英]How to get value of an hidden input from PHP Foreach loop with javascript
我試圖從 PHP 循環中獲取隱藏輸入的值,當它的輸入超出焦點時,它返回所有值數組,當我更改一些代碼時,它只返回第一個值。
這是每個循環的 PHP。
<?php
$s = 0;
foreach ($examSchedule as $key => $student) { ?>
<input type="hidden" id="student" name="student[]" value="<?php echo
$student['student_id'] ?>">
<?php }
?>
它返回這樣的輸入
<input type="hidden" id="student" name="student[]" value="388">
<input type="hidden" id="student" name="student[]" value="389">
<input type="hidden" id="student" name="student[]" value="390">
<input type="hidden" id="student" name="student[]" value="391">
<input type="hidden" id="student" name="student[]" value="392">
<input type="hidden" id="student" name="student[]" value="393">
當我嘗試獲取值時,我得到了包含所有值的數組
$("input").focusout(function(event){
event.preventDefault();
var students= $('[name="student[]"]').map(function () {
return this.value;
});
console.log(students);
}
結果是
n.fn.init(6) ["388", "389", "390", "391", "392", "393", prevObject:
n.fn.init(6), context: document]
當我嘗試使用
$("input").focusout(function(event){
event.preventDefault();
var students = $('#student').val();
console.log(student);
}
它只顯示第一個值。
當 out focus 事件應用於某個輸入字段時,如何獲取特定輸入的值。
在 javascript 代碼中使用“this”關鍵字:
“this”關鍵字將為您提供發生“focusout”事件的輸入元素
$("input").focusout(function(event){
event.preventDefault();
var students = $(this).val();
console.log(student);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.