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

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

Javascript,未捕獲的 ReferenceError:未定義變量

Javascript,未捕獲的 ReferenceError:未定義變量

人到中年有點甜 2023-06-09 15:44:46
我是Javascript的新手,我試圖將兩個參數傳遞給一個函數,該函數然后通過表單提交它們,但是當我單擊充當函數調用者的圖像時,我收到 ReferenceError 。這些是我的輸入、一個文本區域和三個單選按鈕:<label for="searchString">   <input type="text" id="searchString" name="searchString" maxlength="20"> </br></label><section id="searchMode" style="margin-top: 3px; margin-bottom: 2px">   <input type="radio" id="Title" name="searchMode" value="Title"/>   <label for="Title">Title</label>   <input type="radio" id="Author" name="searchMode" value="Author"/>   <label for="Author">Author</label>   <input type="radio" id="Number" name="searchMode" value="Number"/>   <label for="Number">Number</label></section>這是搜索功能:<script>   s = document.getElementById("searchString").value;   opt = document.querySelector('input[name="searchMode"]:checked').value;</script><section style="margin-top: 3px;">   <a href="javascript:search(s,opt)">       <img alt="search" id="searchImage" src="images/search.png" width="22" height="22">   </a></section>我看到的錯誤消息是:未捕獲的 ReferenceError:opt 未定義let我已經嘗試過在沒有or 的情況下聲明這兩個變量,var但這似乎不起作用,我該如何修復我的代碼以免出現此錯誤?編輯:我認為問題可能是我無法將參數從表單正確傳遞到控制器;搜索功能被調用,我沒有收到任何錯誤,但它們被接收為undefined. 這是形式和功能:<form name="searchForm" method="post" action="Dispatcher">   <input type="hidden" name="searchString"/>   <input type="hidden" name="searchMode"/>   <input type="hidden" name="controllerAction" value="ProductsManagement.view"/></form>function search(s,opt){   const f = document.searchForm;   f.searchString.value = s;   f.searchMode.value = opt;   f.submit();}
查看完整描述

3 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

這將修復它的變化:該函數現在是一個函數,它使用 onclick() 而不是 href 運行,要獲取按鈕的值屬性,我們需要使用 getAttribute,最后保留下劃線和紫色(因為我們刪除了href)我們需要添加:style="text-decoration: underline; color: purple;"


<label>

   <input type="text" id="searchString" name="searchString" maxlength="20">

</label>


<section id="searchMode" style="margin-top: 3px; margin-bottom: 2px">

   <input type="radio" id="Title" name="searchMode" value="Title"/>

   <label for="Title">Title</label>

   <input type="radio" id="Author" name="searchMode" value="Author"/>

   <label for="Author">Author</label>

   <input type="radio" id="Number" name="searchMode" value="Number"/>

   <label for="Number">Number</label>

</section>

<script>

  function search(){

    console.log('here')

    let s = document.getElementById("searchString").value;

    let opt = document.querySelector('input[name="searchMode"]:checked')

    if(opt){

       opt = opt.getAttribute('value');

    }

    console.log(opt)

    console.log(s)

  }

</script>


<section style="margin-top: 3px;">

   <a onclick='window.search()' style="text-decoration: underline; color: purple;">

       <img alt="search" id="searchImage" src="images/search.png" width="22" height="22">

   </a>

</section>


查看完整回答
反對 回復 2023-06-09
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

s并且opt未在您的錨點范圍內定義。它們似乎在別處定義。考慮將其更改為以下內容:


<a onclick="search"><img ...></a>


然后,在您的搜索中


<script>

   s = document.getElementById("searchString").value;

   opt = document.querySelector('input[name="searchMode"]:checked').value;

   search(s,opt);

</script>


查看完整回答
反對 回復 2023-06-09
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

你searchString沒有任何價值,所以當你試圖訪問它的價值時,它會給你錯誤。因此,為此您需要為其分配一個值。例如


<label for="searchString">

  <input type="text" id="searchString" name="searchString" maxlength="20" 

  value 

  = "search" > </br>

</label>

完全一樣當你沒有檢查任何選項時radio-button它會給出錯誤,因為在那個特定的時間它不會有任何價值。


因此,您必須為各個元素分配值。


查看完整回答
反對 回復 2023-06-09
  • 3 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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