亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從同一頁面上的 html 表單獲取結果?

如何從同一頁面上的 html 表單獲取結果?

萬千封印 2023-09-18 10:16:53
我有一個搜索框可以從 API 請求信息。但是當我使用搜索框時,我會被定向到一個包含結果的新 HTML 頁面。我想在搜索框正下方的同一頁面上顯示結果。這就是我到目前為止所擁有的。<form  method="get"  id="Search"  action="https://api.hackertarget.com/aslookup/?q="  ONSUBMIT="submit(); return false;">  <input type="hidden" name="sites" value="" />  <input type="hidden" name="k7" value="#ffffff" />  <input type="hidden" name="k8" value="#222222" />  <input type="hidden" name="k9" value="#00278e" />  <input type="hidden" name="kx" value="#20692b" />  <input type="hidden" name="kj" value="#fafafa" />  <input type="hidden" name="kt" value="p" />  <input type="text" name="q" placeholder="INFO" aria-label="Search" />  <button type="submit">Search</button></form>
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

您需要使用AJAX調用。我jQuery.ajax在此示例代碼中使用了方法。您還可以使用瀏覽器的fetchAPI 或XMLHttpRequestAPI。


$(function() {

? $('#Search').on('submit', function() {

? ? event.preventDefault();

? ? var url = 'https://api.hackertarget.com/aslookup/';

? ? var data = $('#Search').serialize();


? ? $.ajax(url + '?' + data).then(function(data) {

? ? ? $('.output').text(data);

? ? });

? });

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<form method="get" id="Search">

? <input type="hidden" name="sites" value="" />

? <input type="hidden" name="k7" value="#ffffff" />

? <input type="hidden" name="k8" value="#222222" />

? <input type="hidden" name="k9" value="#00278e" />

? <input type="hidden" name="kx" value="#20692b" />

? <input type="hidden" name="kj" value="#fafafa" />

? <input type="hidden" name="kt" value="p" />

? <input type="text" name="q" placeholder="INFO" aria-label="Search" />

? <button type="submit">Search</button>

</form>


<pre class="output"></pre>


查看完整回答
反對 回復 2023-09-18
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

<form onsubmit="onsubmitForm(event)">

    <input type="hidden" name="sites" value="" />

    <input type="hidden" name="k7" value="#ffffff" />

    <input type="hidden" name="k8" value="#222222" />

    <input type="hidden" name="k9" value="#00278e" />

    <input type="hidden" name="kx" value="#20692b" />

    <input type="hidden" name="kj" value="#fafafa" />

    <input type="hidden" name="kt" value="p" />

    <input type="text" name="q" placeholder="INFO" aria-label="Search" />

    <button type="submit">Search</button>

</form>


<script src="https://code.jquery.com/jquery-3.4.1.js"

    integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>


<script>

    function onsubmitForm(e) {

        e.preventDefault(); 

        var url = "https://api.hackertarget.com/aslookup?q="+$("input[name='q']").val();

        $.ajax({

            url: url,

            method: "GET",

            success: function (result) {

                console.log(result);

            }

        });

    }



</script>


查看完整回答
反對 回復 2023-09-18
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號