当我得到Null时,如何使用jquery获取输入表单的值? [英] How to get value of input form using jquery while I am geting Null?

查看:79
本文介绍了当我得到Null时,如何使用jquery获取输入表单的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

您能使用以下代码吗?

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


   });  

这篇关于当我得到Null时,如何使用jquery获取输入表单的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆