Php Jquery:获取多个值<;选择>;下拉列表
我正在从db表中获取用户记录,如Php Jquery:获取多个值<;选择>;下拉列表,php,jquery,Php,Jquery,我正在从db表中获取用户记录,如 <?php $row=1; ?> <select id="status<?php echo $row; ?>" name="status<?php echo $row; ?>"> <option value="0">Active</option> <option value="1">Block</option> </select> <
<?php $row=1; ?>
<select id="status<?php echo $row; ?>" name="status<?php echo $row; ?>">
<option value="0">Active</option>
<option value="1">Block</option>
</select>
<?php $row++; ?>
----------------------------------------
Name Status
----------------------------------------
Abc Active
Def Block
Ghi Active
Jkl Block
----------------------------------------
$('status')
将选择节点名为status
的元素。有吗?或者您的意思是$('.status')
?您需要删除for循环,并使用此代码。
我的事件现在是“change”,它将提交两个变量,“id”和“sld”。然后更新php脚本以检查$\u POST['id']和$\u POST['sld'],然后将它们更新到数据库中
将class=“status”添加到每个下拉列表中
$(".status").change(function() {
var sld = $(this).val();
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: { "sld": sld, "id":$(this).attr('id').replace('status','') },
success: function(msg){
alert(msg); // this is the response
}
});
});
我认为您有多个具有相同ID的元素,这将是无效的HTML
为您的选择框放置一个类,然后您可以使用类似的内容
$("select.status").each(function() {
var sld = this.value;
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: "sld="+sld,
success: function(msg){
alert(msg); // this is the response
}
});
});
此外,我在示例中没有发现for循环的任何用途。好的,看起来您出了问题。首先,第2行中的“status”看起来应该是“.status”,每个下拉列表都需要status类。状态ID不起作用,因为您只能有一个。您的PHP代码与问题无关。您应该包括PHP发出的HTML,因为这是浏览器和JavaScript/jQuery所关注的内容。(提示:查看源或检查元素)
$("select.status").each(function() {
var sld = this.value;
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: "sld="+sld,
success: function(msg){
alert(msg); // this is the response
}
});
});