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

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

如何使我的 JavaScript 搜索高級?

如何使我的 JavaScript 搜索高級?

拉風的咖菲貓 2022-11-03 10:57:29
我目前正在進行這樣的 Javascript 搜索: <input type="text" id="link-box"/><input type="button" id="search-button" value="Search"     onclick="window.location = document.getElementById('link-box').value;"/>劇本;    <script type="text/javascript">  function func(){       window.location = document.getElementById('link-box').value;  }  onclick="func();"  </script>這基本上做的是它打開用戶輸入的“值”。(例如;如果用戶輸入 1,它將打開 "www.example.com/1" )。我想要做的是我想在搜索表單中添加另一個文本字段,如下所示:<input type="text" id="link-box2"/>現在我的輸出中有兩個字段和一個提交按鈕。我想要的是,我希望第二個文本字段打開鏈接,為第一個字段中的用戶輸入值添加一個“/”。(例如;之前如果用戶輸入值“1”并打開“www.example.com/1”,現在使用第二個文本字段:如果用戶在第一個文本字段中輸入值“1”和值“2”在第二個文本字段中,它應該打開“www.example.com/1/2”)。簡單地說,“link-box”(第一個文本字段)和“link-box2”(第二個文本字段)之間應該存在某種關系。我是 JavaScript 新手,所以請幫忙?。?!
查看完整描述

3 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

答案是您必須在獲取價值代碼之前使用您的網址。


function func(){ window.location = "https://example.com/"+ document.getElementById('link-box').value; }


或者


function func(){ var searchKey = document.getElementById('link-box').value; window.location = "https://example.com/"+ searchKey; }


如果您還想從其他輸入中添加...您可以這樣做


function func(){

       var searchKey = document.getElementById('link-box').value;

       var searchKey2 = document.getElementById('link-box2').value;

       if(!searchKey2){

       window.location = "https://example.com/"+ searchKey;

       }else{

       window.location = "https://example.com/"+ searchKey + "/" + searchKey2;

       }

  }


查看完整回答
反對 回復 2022-11-03
?
青春有我

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

請試試這個。


HTML:


 <input type="text" id="link-box"/>

 <input type="button" id="search-button" value="Search" onclick="search()"/>

JS:


<script>

const search=()=>{

  let search-value = document.getElementById('link-box').value;

  if(!search-value){

     window.location = `https://example.com/${firstInput}`;

}

  return;

}

</script>


查看完整回答
反對 回復 2022-11-03
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

// On input button click, call function func();

<input type="button" id="search-button" value="Search" 

    onclick="func()"/>


<script>

function func() {

  // Gets the values of the 2 input fields.

  let firstInput = document.getElementById('link-box1').value;

  let secondInput = document.getElementById('link-box2').value;

  

  // Redirect the user to the specified URL with the values from the input boxes.

  window.location = 'https://example.com/' + firstInput + '/' + secondInput;

}


查看完整回答
反對 回復 2022-11-03
  • 3 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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