簡體   English   中英

如何通過ajax發送值輸入類型文本?

[英]How to send value input type text via ajax post?

我有這樣的輸入類型文本

<input type="text" id="test" value="">

我有像這樣的ajax post功能。

$(document).ready(function() {
    $('#summernote').summernote({
        height: 300,                 // set editor height
        minHeight: null,             // set minimum height of editor
        maxHeight: null,             // set maximum height of editor
        focus: true,                  // set focus to editable area after initializing summernote
        callbacks: {
            onImageUpload: function(files) {
                sendFile(files[0]);
            }
        }
    });   

    function sendFile(file, editor, welEditable) {
        document.getElementById("loading_upload_threads_image").style.display = "block";
        data = new FormData();
        data.append("file", file);//You can append as many data as you want. Check mozilla docs for this
        $.ajax({
            data: data,
            type: "POST",
            url: "threads_image_upload.php",
            cache: false,
            contentType: false,
            processData: false,
            success: function(url) {
                $('#summernote').summernote('editor.insertImage', url);
                document.getElementById("loading_upload_threads_image").style.display = "none";
            }
        });
    }
});

我想知道如何將id="test"值發送到我的ajax帖子?

您可以使用append()方法將所需的任何數據添加到FormData對象中 - 正如您的注釋事件所示。 試試這個:

data = new FormData();
data.append("file", file);
data.append('test', $('#test').val());

或者,如果要發送表單中的所有數據,則可以將form元素提供給FormData構造函數。 請注意,項目將被賦予input的名稱作為鍵。

var data = new FormData($('form')[0]);

你可以這樣做:

HTML代碼:

<input type="text" id="test" value="">

JS代碼:

data = new FormData();
data.append("file", file);
data.append("test", $('#test').val());

$.ajax({
    data: data,
    type: "POST",
    url: "threads_image_upload.php",
    cache: false,
    contentType: false,
    processData: false,
    success: function(url) {
        $('#summernote').summernote('editor.insertImage', url);
        document.getElementById("loading_upload_threads_image").style.display = "none";
    }
});

在PHP中,您可以訪問它:

$test = $_POST['test'];

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM