jq uses val() to get the input value invalid

Use id to get the input tag, if you can't get the value of the input, just use the name attribute of the tag.

4 of the following input, too lazy to copy, just post one.

<input id="popup_email_add" name="email" placeholder="输入邮箱">

This is obtained using id and then val(), only the first value can be taken, and the remaining three are undefined.

function user_add(){
    
    
    let Arr=[],
        userName=$('#popup_user_add').val(),
        userEmail=$('popup_email_add').val(),
        password=$('popup_pas_add').val(),
        phone=$('popup_phone_add').val();
        let data1={
    
    "userName": userName,"password": password,"email": userEmail,"phone": phone
        };
        
        Arr.push(data1);
        $.ajax({
    
    
            url:/*[[${#request.getContextPath()} + '/api/admin/account/add']]*/'http://localhost:8080/future-novel/api/admin/account/add',
            type: 'post',
            datatype:'json',
            data: JSON.stringify(Arr),
            contentType: 'application/json; charset=utf-8',
            success: function(data){
    
    
                console.log('添加成功');
                console.log(data)
            },
            error: function(jqXHR){
    
    
                console.log('添加失败');
                console.log(jqXHR.responseJSON.errorMessage);
            }
        })
}

This is obtained with the name attribute, and you can get value(). The specific reason will be posted to the bottom after I find it.

function user_add(){
    
    
    let Arr=[],
        userName=$('#popup_user_add').val(),
        userEmail=$('input[name="email"]').val(),
        password=$('input[name="pas"]').val(),
        phone=$('input[name="phone"]').val();
        let data1={
    
    "userName": userName,"password": password,"email": userEmail,"phone": phone
        };
        Arr.push(data1);
        $.ajax({
    
    
            url:/*[[${#request.getContextPath()} + '/api/admin/account/add']]*/'http://localhost:8080/future-novel/api/admin/account/add',
            type: 'post',
            datatype:'json',
            data: JSON.stringify(Arr),
            contentType: 'application/json; charset=utf-8',
            success: function(data){
    
    
                console.log('添加成功');
                console.log(data)
            },
            error: function(jqXHR){
    
    
                console.log('添加失败');
                console.log(jqXHR.responseJSON.errorMessage);
            }
        })
}

Guess you like

Origin blog.csdn.net/w_____w_____/article/details/107676430