具有一组复选框,例如:
<div class="checkbox checkbox-inline">
<label id="test">
<input type="checkbox" class="icheck" checked value="<?=$skill->name; ?>"> <?=$skill->name; ?>
</label>
</div>
<div class="checkbox checkbox-inline">
<label id="test">
<input type="checkbox" class="icheck" checked value="<?=$skill->name; ?>"> <?=$skill->name; ?>
</label>
</div>
我有以下JS
$('#test').on('click', null, function(){
alert("Clicked");
$(this).remove();
});
我正在尝试的是在用户单击标签时删除各个复选框(及其标签)。
我想要这样的东西:
<div class="checkbox checkbox-inline">
<label id="test" onClick="removeMe()">
<input type="checkbox" class="icheck" checked value="<?=$skill->name; ?>"> <?=$skill->name; ?>
</label>
</div>
在我的JavaScript上:
function removeMe(){
// Do some AJAX GET/POST
$.ajax....
// and finally
$(this).remove() // i am not sure how to do this....
}
谁能指出我正确的方向?谢谢
所以这是我使用的解决方案:
function removeMe(object, skillId, personId){
$.ajax({
type: 'POST',
url: SOME_URL_GOES_HERE,
dataType: 'json',
data: { skill_id: skillId, person_id: personId },
success: function(data, textStatus, jqXHR){
noty({
text: data.message,
layout: 'topRight',
type: data.response
});
$(object).remove();
},
error: function(data){
console.log(data);
noty({
layout: 'topRight',
text: 'Failed to delete record, please try again.',
type: 'error'
});
}
});
}
并建议,我使用.class代替id。
在您的第一个代码中,有多个具有相同ID的标签。每个元素都应该有唯一的ID,这就是为什么您的代码不适用于第一个代码。提供类或其他属性而不是ID
对于第二个代码,您应该执行以下操作:
<div class="checkbox checkbox-inline">
<label id="test" onClick="removeMe(this)"> <!--pass the object of current element-->
<input type="checkbox" class="icheck" checked value="<?=$skill->name; ?>"> <?=$skill->name; ?>
</label>
</div>
和您的JS代码:
function removeMe(that){
// Do some AJAX GET/POST
$.ajax....
$(that).remove();
}
您需要在函数中传递对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句