如何将输入文本框值的参数传递给jQuery中的ajax

问题描述 投票:0回答:2

我的Ajax代码

$(document).ready(function() {
    $("#sub").click(function() {
        $.ajax({
            type: "POST",
            url: "jqueryphp.php",
            data: "txt1=" + txt1,
            success: function(result) {
                $("div").html(result);
            }
        });
    });
});​

这是表单代码。我想将txt1值传递给Ajax

<input type="text" name="txt1" id="txt1" /><br>
<input type="button" name="sub" id="sub" value="click Me" />

我想使用此Ajax函数将txt1值传递给我的php页面。

请告诉我Ajax的data属性究竟会有什么

php javascript jquery ajax parameters
2个回答
15
投票

将数据作为对象而不是字符串发送并检索文本字段的值,使用val():

$(document).ready(function() {
    $("#sub").click(function() {
        $.ajax({
            type: "POST",
            url: "jqueryphp.php",
            data: {
                txt1: $("#txt1").val()
            },
            success: function(result) {
                $("div").html(result);
            }
        });
    });
});​

http://api.jquery.com/val/


2
投票

你的意思是:


$(document).ready(function() {
    $("#sub").click(function() {
    var txt1 = $("#txt1").val(); //textbox value
        $.ajax({
            type: "POST",
            url: "jqueryphp.php",
            cache: false,
            data: "txt1=" + txt1,
            dataType: "html",
            success: function(result) {
                $("div").html(result);
            }
        });
    });
});​


© www.soinside.com 2019 - 2024. All rights reserved.