如何从文本框jquery和ajax中获取值?

时间:2014-10-27 08:42:07

标签: javascript jquery ajax json

我正在尝试从文本框中获取值并将其发布到表中,但是当我尝试console.log时,"数据"成功内部:功能(数据),它显示空数据。但如果我在$ .ajax之外控制它,它会显示正确的值。

这是我的jquery函数

function SaveData() {

            var data = $("#inputToDo").val();
            $.ajax({
                type: "POST",
                dataType: "json",
                success: function (data) {
                    var row = "";
                    $.each(data, function (index, value) {
                        console.log(data);
                        row += "<tr><td>" + data + "</td></tr>";
                        $("#bod").html(row);  
                    });
                }
            });
            console.log(data);
            data = $("#inputToDo").val("");
            $("#inputToDo").focus();

    }

我的HTML:

<input id="inputToDo" name="inputToDo" />
<button id="buttonSave" name="buttonSave" onclick="SaveData()">
    Save
</button>

<table id="tableToDo" border="1">
    <tr>
        <td id ="ToDo"></td>
        <td id ="Execution"></td>
    </tr>
    <tbody id="bod"></tbody>
</table>

任何帮助将不胜感激。谢谢

5 个答案:

答案 0 :(得分:0)

var data = $("#inputToDo").val();       // outer variable

success: function (data)                //inner variable
{            

            //please note here you are access data variable of success function.
            // which is passed as parameter so outer data variable will always 
            //get override by inner variable
}

解决方案:尝试更改外部变量的名称,然后尝试

答案 1 :(得分:0)

您正在使用数据作为变量(var data = $ ( "#inputToDo" ). val ();)以及作为参数(success : function ( data ) {)。

当您在成功案例中尝试时,数据将引用ajax成功响应,在外侧它将引用数据变量,这是您的&#34; #inputToDo&#34;值

而不是使用可变数据,您可以将其作为inputToDoVal,如下面的代码

var inputToDoVal = $ ( "#inputToDo" ). val ();

答案 2 :(得分:0)

我为你做了一个工作小提琴。

我已经基本改变了一点JavaScript,我认为你已经包含了jquery ......

$('document').ready(function(){
    SaveData = function(){
        var data = $("#inputToDo").val();
        alert(data);
    }
}

这是Fiddle

答案 3 :(得分:0)

*  <div class="row">
                <div class="form-group">
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            To Account Number:
                           <input type="text" id="txtcaccno" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <input type="button" class="btn btn-success" class="form-control" onclick="search();" value="View" />
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Account Name: 
                            <input type="text" id="txtcaccname" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Account Phone: 
                            <input type="text" id="txtcaccphone" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Account Email: 
                            <input type="text" id="txtcaccemail" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Account Type: 
                           <input type="text" id="txtcacctype" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Balace Amount: 
                            <input type="text" id="txtcamou" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <label>
                            Current Balace Amount: 
                            <input type="text" id="txtcurrcamou" runat="server" ClientIdMode="static" class="form-control" autocomplete="off" />
                        </label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4">
                        <input type="button" class="btn btn-success" class="form-control" onclick="appro();" value="Approved" />
                    </div>
                </div>
            </div>*

答案 4 :(得分:0)

替换下面的代码

b

ex5 = case undefined of ~(a,b) -> print "works fine"
ex6 = g undefined
g ~(a,b) = print "also works fine"

ex7 = case undefined of ~(a,b) -> print $ "But trying " ++ show (a+b) ++ " would fail"

就可以了。 Append用于将jquery中的数据添加到html。如果解决了,请标记正确的答案。