我有以下的HTML代码
{% for state in us_states %}
<input id="{{state.0}}" type="checkbox" name="{{state.0}}" class="value_check"/>
<span style="padding-left:20px;padding-right:20px;">{{state.1}}</span>
<input id="id_{{state.1}}" type="text" name="{{state.0}}" class="input-block-level-b iph_lvl_b span2 must_be_float" placeholder="0.0">
{% endfor %}
所以我正在尝试的是,当用户在上面的文本输入字段中选择一些值时,如果未选中该复选框,那么我应该向他发出警告"Please select the checkbox"
。所以我想写一个jquery onfocusout function
,想要查看是否选中了复选框
注意
我想找到找到前面元素及其值的方法,因为你可以在我的代码中看到我从for循环动态生成它们,所以通常想要找到前一个复选框元素并检查是否检查,如果没有,则显示警告
$(document).ready(function(){
$('.value_check').focusout(function() {
console.log(this.id);
var prev = $('.value_check').prev()
console.log(prev.attr(id));
});
});
答案 0 :(得分:0)
焦点输出功能称为blur
,您应该在实际文本字段上使用blur
,而不是复选框,所以类似于:
$("input[id^=id_]").blur(function() {
var checked = $(this).siblings(".value_check").prop("checked");
if (!checked)
alert("Didnt check zeh box!");
});
$(".value_check").change(function() {
if (!this.checked)
alert("You must check the box to continue");
});