开发者

Jquery: Get value of more than one <select> drop-down

I am fetching user record from db-table like

<?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+开发者_JAVA百科+; ?>

----------------------------------------
  Name                    Status
----------------------------------------
  Abc                     Active
  Def                     Block
  Ghi                     Active
  Jkl                     Block
----------------------------------------

where status is drop-down with two status for each user and If I want to change the status of any user then I select an option from that drop-down and at the same time the status must update in db-table. For this I coded:

for(var r=1; r<=4; r++){
    $("status").each(function() {
        var sld = $("#status").val();
        alert(sld);
        $.ajax({
            type: "POST",
            url: "response.php",
            data: "sld="+sld,
            success: function(msg){
                alert(msg); // this is the response
            }
        });
    });
}

but for loop does not create script for each drop-down....


You'll need to remove that for loop, and use this code. My event is now "change" and it will submit two variables, "id" and "sld". You then update your php script to check $_POST['id'] and $_POST['sld'] and then update these into the database.

add class="status" to each of the drop downs.

$(".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
            }
        });
    });


$('status') would select elements that have the node name of status. Do you have that? Or did you mean $('.status')?


I think you are having more than one element with the same ID which will be an invalid HTML.

Put a class for your select boxes and then you can use something like this.

$("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
        }
    });
});

Also I don't find any use of the for loop in the example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜