开发者

jQuery Correct Syntax in simple Ajax request

开发者 https://www.devze.com 2023-03-22 10:48 出处:网络
I don\'tunderstand why data: \'txt=\' + $(\'#nombre\').value, 开发者_C百科doesn\'t work but data: \'txt=\' + document.formulario.nombre.value, works.

I don't understand why data: 'txt=' + $('#nombre').value, 开发者_C百科doesn't work but data: 'txt=' + document.formulario.nombre.value, works.

Here is the js code:

function validarNombre(){
    $.ajax({
        url: 'http://redwow.net/jqueryajax.php',
        type: 'GET',
        data: 'txt=' + $('#nombre').value,
        success: function(datos){
                alert(datos);
        },
    });
};

And here is the html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Ejemplo Ajax Personalizado</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="jqueryajax.js"></script>

</head>
<body>
    <form name="formulario">
        <table>
            <tr>
                <td>Nombre:</td><td><input type="text" id="nombre" name="nombre" onKeyPress="validarNombre()"/></td>
            </tr><tr style="visibility:hidden;">
                <td>E-mail:</td><td><input type="text" id="email"/></td>
            </tr>
        </table>
        <input type="submit" value="Enviar"/>
    </form>
</body>
</html>

Thanks.


It should be data: 'txt=' + $('#nombre').val() and not data: 'txt=' + $('#nombre').value


do something like this

  var sm = $('#nombre').val();
function validarNombre(){
    $.ajax({
        url: 'http://redwow.net/jqueryajax.php',
        type: 'GET',
        data: 'txt=' + sm ,
        success: function(datos){
                alert(datos);
        },
    });
};
0

精彩评论

暂无评论...
验证码 换一张
取 消