gpt4 book ai didi

php - Jquery将不同的id值传递给包含变量的通用函数

转载 作者:行者123 更新时间:2023-12-01 04:18:40 24 4
gpt4 key购买 nike

第一次来,先尝试一下 php/jquery

此代码正在运行:

$('#update1').click(function(){
var name= $('#bandname-1').val();
var bandid = $('#bid-1').val();
var mbid = $('#mbid-1').val();
$('#msg').text('updating...');
$.post('func/update_bandname.php',{
name: name,
bandid: bandid,
mbid: mbid
}, function(data){
$('#msg1').text(data);
});
});

这是我的PHP:-剪断

echo "<td>";
echo "<input type=\"text\" name=\"bandname-".$i."\" id=\"bandname-".$i."\" value=\"".$name ."\"/>&nbsp;";
echo "<input id=\"update".$i."\" type=\"button\" name=\"update".$i."\" value=\"update\" class=\"dp\"/>";
echo "<span id=\"msg".$i."\"></span><input type=hidden id=\"bid-".$i."\" value=\"".$_REQUEST[bid] ."\" name=bid-".$i."\" />";
echo "<input type=hidden id=\"mbid-".$i."\" value=\"".$bandid."\" name=mbid-".$i."\" />";
echo "</td>";
echo "</tr>";
$i++;

数据库将大量记录返回到该表中,我创建了 5 个函数 update1、update2 等

有没有一种方法可以修改我的 jquery 以将不同的 id 值传递给一个函数?而不是一遍又一遍地复制函数?

谢谢

最佳答案

为每个元素分配一个公共(public)类和 data- 属性:

PHP:

echo '<input class="updatebutton dp" data-idnum="'.$i.'" id="update'.$i.'" type="button" name="update'.$i.'" value="update">';

JS:

$('.updatebutton').click(function() {
var id = $(this).data('idnum'),
name= $('#bandname-'+id).val(),
bandid = $('#bid-'+id).val(),
mbid = $('#mbid-'+id).val();
$('#msg').text('updating...');
$.post('func/update_bandname.php', {
name: name,
bandid: bandid ,
mbid:mbid
}, function(data) {
$('#msg'+id).text(data);
});
});

关于php - Jquery将不同的id值传递给包含变量的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com