开发者

Replace inputs based on JSON data returned

开发者 https://www.devze.com 2023-03-12 07:59 出处:网络
I am submitting some form data via ajax and getting back a JSON array of id numbers effected. Using these id numbers I need to replace the input checkbox with coresponding value with a div element th

I am submitting some form data via ajax and getting back a JSON array of id numbers effected.

Using these id numbers I need to replace the input checkbox with coresponding value with a div element that contains a confirm message.

My HTML looks like this:

<input type="checkbox" name="users[]" value="26">
<input type="checkbox" name="users[]" value="27">
<input type="checkbox" name="users[]" value="28">

The data returned from the ajax submit looks like this:

["28","26"]开发者_JAVA技巧

I need to replace the input elements with values matching these ids with a div element that looks like this:

<div class="invited">Invited!</div>


I like this way a bit better:

var ids=[26,28];

$.each(ids,function(index, el){
   $("input[value='"+el+"']").replaceWith('<div class="invited">Invited!</div>')
});


var yourArray = ['28', '26'];

$('input[name="users[]"]').each(function(i)
{
    for (var i = 0, ilen = yourArray.length; i < ilen; i++)
    {
        if ($(this).val() == yourArray[i])
        {
            $(this).replaceWith('<div class="invited">Invited!</div>');
        }
    }
});
0

精彩评论

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