首页 > 解决方案 > 创建包含两个选择的链接

问题描述

我有两个选择,我想用两者的值创建一个链接。但问题是,如果我进行选择更改,链接不会刷新。

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;
}

选择更改时如何刷新链接?

标签: javascripthtmljqueryinput

解决方案


监听更改事件并在此之后进行测试。另外,使用 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);
  });
})

推荐阅读