简体   繁体   English

在jQuery中的前一个元素之前选择输入元素

[英]select input element before previous element in jquery

I had the below html code 我有下面的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 %}

So what i am trying is, when the user selects some value in the text input field above, and if the checkbox is not checked, then i should make an alert to him that "Please select the checkbox" . 因此,我正在尝试的是,当用户在上面的文本输入字段中选择某个值时,并且如果未选中该复选框,那么我应该向他发出"Please select the checkbox"的警报。 so i want to write a jquery onfocusout function , and want to find whether the checkbox is checked or not 所以我想编写一个jquery onfocusout function ,并想查找是否已选中该复选框

Note 注意

I want to find the in the way of finding the previous elements and their values, becasue as u can see in my code that i am generating them dynamically from for loop, so usually want to find the previous previous checkbox element and check whther its checked or not, if not then display an alert 我想找到查找先前元素及其值的方法,因为正如您在我的代码中看到的那样,我是从for循环动态生成它们的,所以通常希望查找先前的先前checkbox元素并检查其是否选中是否,如果没有,则显示警报

$(document).ready(function(){
      $('.value_check').focusout(function() {
            console.log(this.id); 
            var prev = $('.value_check').prev()
            console.log(prev.attr(id)); 
      });        
   });

The focus out function is called a blur , and you should be using the blur on the actual text field, not the checkbox, so something like: 聚焦功能称为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");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM