問題描述
帶有選項值用法的 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.