当我得到Null时,如何使用jquery获取输入表单的值?

时间:2019-07-04 04:47:18

标签: jquery html json ajax get

我尝试了所有技巧,但没有一个对我有用,我正在尝试使用jquery获取表单值,并尝试在ajax get request中使用它,但它不显示null。已超过18个小时。我将非常感谢你们。

$(document).ready(function(){
    var text;
    $("#button").click(function () {
    window.value=$("#q").val();
    alert(window.value);


   });   
$.ajax({

    url: "/search",
    data:  {q:$("#q").val()},
    type: "GET", 
    dataType : "json",
    success: function(result) { 
    var event_data = '';
    for (var i = 0; i < result.length; i++) {
                event_data += '<tr>';
                event_data += '<td>' + '<img src=' +result[i]['video']['thumbnail_src']+ '></img>' +'</td>';
                event_data += '<td>' +result[i]['video']['title']+'</td>';
                event_data += '<td>' +result[i]['video']['duration']+ '</td>';
                event_data += '<td>' + '<a href' +result[i]['video']['url']+ '>'+ "Download" +'</a>' +'</td>';
                event_data += '</tr>';
     }  $("#list_table_json").append(event_data);
    },
    error: function(jqxhr, status, exception) {
             alert('Exception:', exception);
         },
    always: function( xhr, status ) {
      alert( "The request is complete!" );
        }
    });
});

HTML文件:

<form class="down-form" id="form1" name='form1'  method=" " action=" ">
                <input type="text" id="q"  name="q" placeholder="Search or Paste Link Here... " required class="form-control form-control-lg"/>
                <button type="submit" id="button" class="btn btn-success mt-3">Search</button>
            </form>

1 个答案:

答案 0 :(得分:0)

您能使用以下代码吗?

$(document).ready(function(){
    var text;
    $("#button").on('click',function (e) {
    e.preventDefault();
   var q=$("#q").val();
    $.ajax({

    url: "/search",
    data:  {q:q},
    type: "GET", 
    dataType : "json",
    success: function(result) { 
    var event_data = '';
    for (var i = 0; i < result.length; i++) {
                event_data += '<tr>';
                event_data += '<td>' + '<img src=' +result[i]['video']['thumbnail_src']+ '></img>' +'</td>';
                event_data += '<td>' +result[i]['video']['title']+'</td>';
                event_data += '<td>' +result[i]['video']['duration']+ '</td>';
                event_data += '<td>' + '<a href' +result[i]['video']['url']+ '>'+ "Download" +'</a>' +'</td>';
                event_data += '</tr>';
     }  $("#list_table_json").append(event_data);
    },
    error: function(jqxhr, status, exception) {
             alert('Exception:', exception);
         },
    always: function( xhr, status ) {
      alert( "The request is complete!" );
        }
    });
});


   });