javascript - jquery uses the for method to obtain the page attribute value of a repeated name and cannot get help.
PHPz
PHPz 2017-05-19 10:17:03
0
3
498

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~

PHPz
PHPz

学习是最好的投资!

reply all(3)
巴扎黑

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

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

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template