samedi 3 mars 2018

How do I get an element from the list of element in js, which has a custom attribute?

I want to get all element that has costume value attribute from list of same element of j-Query.

I have this following HTML form :

<label>name <input type="text" name="name" value="value"></label>
<label>value1 <input type="radio" name="name1" value="value1"></label>
<label>value2 <input type="radio" name="name1" value="value2"></label>
<label>value3 <input type="radio" name="name1" value="value3"></label>
<label>value4 <input type="checkbox" name="name2[]" value="value4"></label>
<label>value5 <input type="checkbox" name="name2[]" value="value5"></label>

I did try this following code :

form = $('form');
form_data = {
    'name':'test value',
    'name1':'value2',
    'name2[check]':'value5',
};
$.each(form_data,function(i,v){
    input = form.find('[name="'+i+'"]');
    inputtype = input.attr('type');

    if(inputtype == 'checkbox' && inputtype == 'radio'){
        input.has('[value="'+v+'"]').prop('checked',true);
    }else{
        input.val(v);
    }
});

But when i try input.has('[value="'+v+'"]') for get same element attribute then i did not return any element to checked that element.. ! any solution for this..?




Aucun commentaire:

Enregistrer un commentaire