我有一个包含以下PDF文件的文件夹:
0001 - 101 - Almonds 1kg.pdf
0001 - 396 - Almonds 6 x 1kg.pdf
0001 - 400 - Almonds 10 x 1kg.pdf
我试图创建一个window.open
函数来跟踪目录并打开从下拉菜单中选择的特定PDF。
作为示例,这是文件的存储位置: C:\ Users \ test \ Desktop \ work
这是我的代码:
document.addEventListener("DOMContentLoaded", function(){
document.querySelector('.btn-1').addEventListener('click', function(){
var formOne = document.querySelector('#Pcode');
var formTwo = document.querySelector('#Wcode');
var pcode = document.querySelector('.Pcode-val');
var wcode = document.querySelector('.Wcode-val');
var opt = getSelectedOption(pcode);
var wpt = getSelectedOption(wcode);
function getSelectedOption(pcode) {
var opt;
for ( var i = 0, len = pcode.options.length; i < len; i++ ) {
opt = pcode.options[i];
if ( opt.selected === true ) {
break;
}
}
return opt;
};
function getSelectedOption2(wcode) {
var wpt;
for ( var i = 0, len = wcode.options.length; i < len; i++ ) {
opt = wcode.options[i];
if ( opt.selected === true ) {
break;
}
}
return wpt;
};
window.open(
'C:\Users\test\Desktop\work' + opt.value + ' - ' + wpt.value,
'_blank' // <- This is what makes it open in a new window.
);
});
});
<!DOCTYPE html>
<html>
<head>
<title>Specs</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<form id="Pcode">
<select class="Pcode-val">
<option value="0001">Almonds</option>
<option value="0002">Ground Almonds</option>
</select>
<button type="button" class="btn-1">Submit</button>
</form>
<form id="Wcode">
<select class="Wcode-val">
<option value="101">1kg</option>
</select>
</form>
</body>
</html>
window.open(
'C:\Users\test\Desktop\work' + opt.value + ' - ' + wpt.value,
'_blank' // <- This is what makes it open in a new window.
);
我尝试过的是,放入目录并添加opt和wpt值(这些是HTML文件中的options的值)。这是我所知道的。如何在不完全输入字符串但部分字符串且仍以0001-101.pdf的形式打开0001-101 Almonds.pdf文件的情况下打开文件?