简体   繁体   中英

How to load a textbox using ajax

Below is my Jquery and ajax code to load text box data sending from server class. Am getting response so that alert inside success function executed data sent from the server browser console why its not loading to textbox The error am getting

$(document).ready(function () {
                $('#userCheck').change(function (event) {
                    if(!$(this).is(":checked"))
                        return;
                    var userNumber = $('#userNumber').val();           
                    if(userNumber != ""){
                        $.ajax({
                            type : 'GET',
                            url : 'populateTextBox',
                            dataType : 'json',
                            data : {
                                userNumber : $('#userNumber').val()
                                },
                            success : function(data, success) {
                                alert("babu code")
                                console.log(data)
                                /* var json = data; */
                                $('#Name').value(data.name);                        
                            },
                            error : function(data, success) {
                                console.log(data)
                                document.getElementById("er").innerHTML = "Problem to upload a name";
                                er.style.color='red';
                                $('#er').show();                        
                            }
                        });
                    }else {
                        //FIXME: Handle this properly
                        alert("Enter User Number")
                    }
                });
            });

<s:form action="populateTextBox">
        <s:textfield name="userNumber" id="userNumber"></s:textfield>       
        <s:checkbox id="userCheck" name="userCheck"></s:checkbox>
        <s:textfield name="userName" id="Name"></s:textfield>
        <td><p id="er"></td>
        <s:submit/>
    </s:form>

Try:

$('#Name').val(data);

If console.log(data) is returning pradeep in your console, then that is all you need.

And, as Grégoire Fruleux also pointed out in the comments, use .val() instead of .value()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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