自动填充输入Javascript

时间:2014-03-06 12:22:42

标签: javascript php jquery

我有这个脚本来填充链接的输入框,但它只加载value =“自动填充成功”。我想要从像get-fill.php?= names

这样的php文件加载动态的东西
<input type="text" name="name_textbox" id="id_textbox" />   
<a onclick="autofill()" href="#">hey</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript">
    function autofill(){
        var object = document.getElementsByName('name_textbox');
        object.item(0).value="Autofill successful.";
    }


</script>

1 个答案:

答案 0 :(得分:0)

GET-fill.php:

if(isset($_POST['names'])){
    echo 'Autofill successful.';
} else {
    echo 'names not set';
}

JS:

function autofill(){
    $.ajax({
        url: "get-fill.php",
        type: "POST",
        dataType: 'text',
        data: {names: true},
        success: function(text){
            $("input[name=name_textbox]").val(text);
        }
    });
}

HTML

<input type="text" name="name_textbox" id="id_textbox" />   
<a onclick="autofill()" href="#">hey</a>