search

Home  >  Q&A  >  body text

javascript - jquery uses the for method to obtain the page attribute value of a repeated name and cannot get help.

The situation is as follows: I have a form that needs to dynamically add input items, and the name of each input item is the same. Since non-null verification is required, consider using jquery to obtain the value of each element and judge it.
code show as below:

$("#reg").click(function(){

        for(var i=0;i <= reNum - 1;i++){
            alert(i);
            alert($("input[name='userLoginNo']:eq(i)").val());
        }
    })

Among them, reg is the submit button (type is button), which will be verified after clicking. If all are not empty, submit. But when I want to capture the value of each input, I can only spit out undefined.
If you change the i in alert($("input[name='userLoginNo']:eq(i)").val()); to a direct number, such as alert($("input[name= 'userLoginNo']:eq(0)").val()); can display the input value normally
Please help us find out why, thank you very much~

PHPzPHPz2769 days ago525

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-19 10:19:03

    The variables in your eq are strings, not variables. You need to use "+i+"

    reply
    0
  • 阿神

    阿神2017-05-19 10:19:03

    var check = false;
    $("input[name='userLoginNo']").each(function(){
        if($("input[name='userLoginNo']").val() == ''){
            check = true;
        }
        //指针指向
        $(this).focus();
        //结束循环 
        if(check){
        alert('不可为空');
        }
        return;
    })
    

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:19:03

    The variable i should be used as a dynamically changing parameter instead of being placed in double quotes as a fixed string i. Change it to this and you can read: $("input[name='userLoginNo']:eq("+i+")").val(). Tested myself.

    reply
    0
  • Cancelreply