帶有選項值用法的 HTML 和 js (HTML and js with option value usage)


問題描述

帶有選項值用法的 HTML 和 js (HTML and js with option value usage)

I have first a  with 3 option value in it. When I select one of the values, I want it to remember which one I picked and once I press my button the javascript will open a specific url.

Some code sample.

       <select id="dd‑files">
        <option value="asdf">Pick:</option>
        <option value="url1">File1</option>
        <option value="url2">File2</option>
        <option value="url3">File3</option>
       </select>
      </section>
    </div>
<br>
        <div class="modal‑footer">
            <a class="btn btn‑primary get‑data" href="#">Button</a>
        </div>

javascript This javascript directily just to know how it all works, so I just tested it. 

$(".get‑data").on("click",function() {
  var location = 'http://hardcodedurl.com';
  window.open(location);

Then I have another function, which when I pick one option value, stores it and open it directly, but instead of open it directly, I would like my second script just to open when my button ".get‑data" is pressed.

$(function () {
  $('#dd‑files').on('change', function() {
    debugger;
    var url = $(this).val();
    if (url) {
      window.location = url;
    }
    return false;
  })
})

Any advice for a newbie?

‑‑‑‑‑

參考解法

方法 1:

Not clear why do you need on('change') event. To make it open when button is pressed, you just need something like this:

$(".get‑data").on("click",function() {
  var location = $('#dd‑files').val(); 
  if(location) {
     window.open(location);
  }
}

Just take an url from #dd‑files directly. 

(by WeeklyDadViktor S.)

參考文件

  1. HTML and js with option value usage (CC BY‑SA 3.0/4.0)

#javascript #html






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論