ton*_*lfx 2 php jquery textarea
我在每个表行上都有唯一ID的textarea区域.如何使用javascript检索该唯一ID?
PHP:
$query = $db->query("SELECT * FROM bs_events WHERE eventDate = '".$date."'");
while($row = $query->fetch_array(MYSQLI_ASSOC)){
echo '<textarea id=\"att_name_" . $row['id'] . "\" style=\"width:300px\"></textarea>";'
}
Run Code Online (Sandbox Code Playgroud)
PHP输出:
<textarea id="att_name_1" style="width:300px">
<textarea id="att_name_2" style="width:300px">
<textarea id="att_name_3" style="width:300px">
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$(document).ready(function(){
$("#book_event").submit(function(){
id = event.target.id.replace('att_name_','');
$.post("Scripts/book_event.php", {
att_name: $("att_name_"+id).val(),
}, function(data){
if(data.success) {
$("#err").text(data.message).fadeIn("slow");
}
}, "json");
});
});
Run Code Online (Sandbox Code Playgroud)
在我看来,你正在命名你的textareas以与数据库条目相关联,然后尝试进行更新并将这些值传回.假设textareas采用您提交的形式,您可以使用:
$('#myform').submit(function(e){
// find each of those text areas
$(this).find('textarea[id^=att_name]').each(function(i,e){
//
// from here-in, e now represents one of those textareas
//
// now submit the update
$.post('Scripts/book_event.php',{
att_name: $(e).val()
},function(data){
if (!data.success)
$("#err").text(data.message).fadeIn("slow");
},'json');
});
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
理想情况下,如果您希望使用AJAX将更新/更改推送回服务器,您可以查看.serialize()并推送所有表单.然后,在服务器端,您将获得$_POST['att_name_1']可用于实际更新的标准值.例如
// .serialize() example
$('#myform').submit(function(e){
$.post('Scripts/book_event.php',$(this).serialize(),function(data){
if (!data.success)
$("#err").text(data.message).fadeIn("slow");
});
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4054 次 |
| 最近记录: |