How to get value of input form using jquery while I am geting Null?

送分小仙女□ 提交于 2019-12-11 14:29:39

问题


I have tried all the tricks but none worked for me,I am trying to get form value using jquery and trying to use it in ajax get request but its not working it showing null.Its been more than 18 hours .I shall be very thankful to you guys.

$(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 file:

<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:


Can you plz use this code:

$(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!" );
        }
    });
});


   });  


来源:https://stackoverflow.com/questions/56880793/how-to-get-value-of-input-form-using-jquery-while-i-am-geting-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!