2017-02-23 301 views
-1

У меня есть этот код, но он работает только с <img>. Он изменяет изображение и добавляет значение к флажку. Я хотел бы, чтобы он работал с <i class='fa'></i>. Что мне нужно изменить?Изменить <i> при нажатии на

$(".img-checkbox").click(function() { 
 
    var img = $(this); 
 
    alert(img.prev().prop('checked')); 
 
    if (img.prev().prop("checked")) { 
 
    img.css({ 
 
     'opacity': '0.3' 
 
    }); 
 
    } else { 
 
    img.css({ 
 
     'opacity': '1.0' 
 
    }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="fa fa-phone fa-4x" class="img-responsive img-checkbox"></i> 
 
</label> 
 

 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="fa fa-user fa-4x" class="img-responsive img-checkbox"></i> 
 
</label> 
 

 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="fa fa-mail fa-4x" class="img-responsive img-checkbox"></i> 
 
</label>

+2

Не могли бы вы сделать описание вашего вопроса лучше? Я не могу понять предложения. –

+0

Также обратите внимание, что атрибут ['hidden'] (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden) не поддерживается в

ответ

0

Вы устанавливаете class два раза в ваших <i> элементов, что делает его игнорировать второй class назначение и JQuery не распознает элементы будучи щелкнул. Вы не можете использовать атрибут более одного раза для каждого элемента. Изменение

<i class="fa fa-user fa-4x" class="img-responsive img-checkbox"></i> 

в

<i class="fa fa-user fa-4x img-responsive img-checkbox"></i> 

$(".img-checkbox").click(function() { 
 
    var img = $(this); 
 
    if (img.prev().prop("checked")) { 
 
    img.css({ 
 
     'opacity': '0.3' 
 
    }); 
 
    } else { 
 
    img.css({ 
 
     'opacity': '1.0' 
 
    }); 
 
    } 
 
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="fa fa-phone fa-4x img-responsive img-checkbox"></i> 
 
</label> 
 

 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="img-responsive img-checkbox fa fa-user fa-4x"></i> 
 
</label> 
 

 
<label class="checkbox-inline"> 
 
    <input type="checkbox" name="qRequirements" hidden> 
 
    <i class="fa fa-mail fa-4x img-responsive img-checkbox"></i> 
 
</label>

+0

Спасибо, но почему флажок не получает значение, когда я нажимаю на элемент ? – roi

+0

Ничто в вашем коде не устанавливает значение для этого флажка. Вы можете добавить img.prev(). Val ("yourValue"); установить это. @roi – baao

Смежные вопросы