Replace inputs based on JSON data returned
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>');
}
}
});
精彩评论