将Javascript变量附加到Jquery AJAX URL

时间:2014-10-01 13:24:07

标签: javascript jquery ajax variables

我想将textboxValue附加到URL - test.php

网址应为test.php?variable =" textboxValue"

var textboxValue = document.getElementById("textbox").value;

window.onload = function() {

window.addEventListener('shake', shakeEventDidOccur, false);

//define a custom method to fire when shake occurs.
function shakeEventDidOccur () {
    $.ajax({url:"test.php?value=var textboxValue"});

}

我该怎么做?

4 个答案:

答案 0 :(得分:3)

非常确定你可以利用data属性来做这类事情......

$.ajax({
    url: "test.php",
    data: { value: textboxValue }
});

答案 1 :(得分:1)

$.ajax({url:"test.php?value="+textboxValue});

OR

$.ajax(
    {url:"test.php"},
    {data:{value:textboxValue}}

);

答案 2 :(得分:0)

你可以简单地连接。

url:"test.php?value="+$('#id').value()}

答案 3 :(得分:0)

如果你将使用$ .ajax,你需要包含jQuery源代码,然后你也可以使用jQuery函数来做到这一点。

试试这个:

//Into your html "head" tag you need to have this:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

//And then in your code you could have something like this:
$(document).ready(function () {
    window.addEventListener('shake', shakeEventDidOccur, false); //You have now the task to find what jQuery function or method could be replacing this to make your code integrated completly

    //define a custom method to fire when shake occurs.
    function shakeEventDidOccur () {
        var textboxValue = $('#textbox').val(); //Where textbox is the "id" attr of your textbox
        $.ajax({url:"test.php"
                type: 'GET', //can be POST too
                url: '/wp-admin/admin-ajax.php',
                data: { 'value': textboxValue},
                success: function (request_data) {
                    //some code to execute after the call as a callback
                }
        });
    }
});