在ajax数据中发布文本框值

时间:2017-08-28 19:48:16

标签: ajax

尝试在文本框中的ajax发布数据中插入值但是如果设置文本框的默认值则不起作用然后它显示值但我需要处理用户输入值。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <?php 
        function test($data){
            return $data;
        }

        if (isset($_POST['callFunc1'])) {
            echo test($_POST['callFunc1']);

        }
    ?>
    <form>
    <input type="text" id="name" />
    <div onclick="myfun()" >Clck Me</div>
    </form>


    <script>
        var names = $("#name").val();

        function myfun(){
            $.ajax({
            url: 'index.php',
            type: 'post',
            data: { "callFunc1": names},
            success: function(response) { console.log(response); }
        });
        }
    </script>

1 个答案:

答案 0 :(得分:2)

我认为您应该将var names = $("#name").val();放入myfun()函数中,如下所示:

function myfun(){

    var names = $("#name").val();

    $.ajax({
      url: 'index.php',
      type: 'post',
      data: { "callFunc1": names},
      success: function(response) { console.log(response); }
    });
}
相关问题