javascript - 创建包含两个选择的链接
问题描述
我有两个选择,我想用两者的值创建一个链接。但问题是,如果我进行选择更改,链接不会刷新。
const seOne = $('select[name=selectionOne] option').filter(':selected').val();
const seTwo = $('select[name=selectionTwo] option').filter(':selected').val();
if (seOne != null && seTwo != null) {
const goThereButton = document.getElementById("goThereButton");
goThereButton.textContent = "View";
goThereButton.classList.remove("btn-secondary");
goThereButton.classList.add("btn-primary");
goThereButton.href = appLink + 'page?scd=content&id=' + seOne + '&hsg=' + seTwo;
}
选择更改时如何刷新链接?
解决方案
监听更改事件并在此之后进行测试。另外,使用 jQuery,您可以将所有这些函数链接到#goThereButton
$(document).ready(function() {
$('select[name=selectionOne], select[name=selectionTwo]').change(function() {
let v1 = $('select[name=selectionOne]').val(), v2 = $('select[name=selectionTwo]').val();
if (v1 && v2) $("#goThereButton").text("View").removeClass("btn-secondary").addClass("btn-primary").attr('href', appLink + 'page?scd=content&id=' + v1 + '&hsg=' + v2);
});
})
推荐阅读
- python - 按部分从 Wikipedia API 获取纯文本
- javascript - 你如何使用 JavaScript 删除两个相同的字符以及它们之间的任何内容?
- c# - 将字符串转换为 TimeSpan 并将其保存到 MS Access 数据库
- reactjs - 在 Recharts 中选择 X 轴上的范围
- javascript - HTML 将 .txt 保存在本地服务器
- java - 如果我们可以将一个 Integer 对象类型转换为 Object 并将一个 Integer 拆箱为 int,为什么我们不能将同一个对象拆箱为 int 呢?
- javascript - 如何访问作为字符串保存在数组中的元素的 id 标签和其他元素值?
- matlab - 是否有任何命令可以在 MATLAB 的向量中找到前 5 个值的平均值,然后是下一个 5 个值,总共 1000 个值
- numpy - 在 PyTorch 中设置浮点类型时,张量类型和 dtype 有什么区别,什么时候应该设置一个而不是另一个?
- javascript - 是否可以从客户端解决跨域资源共享问题?