如何在填写输入字段并选中复选框后向我的网址添加参数而不提交表单

我正在编写一个简单的费用计算器,我有一些复选框和文本输入,我需要将输入的值添加到我的网址中,以便我可以将网址发送给具有相同值的人。我已在字段值中设置参数,例如:

value="<?= isset($_GET['collect']) ? $_GET['collect'] : '0' ?>"

当我手动在网址中输入参数时,一切正常,例如:

myurl.com/?collect=200&collectBudget=50

如果选中复选框或在输入字段中输入值,我需要在 url 中动态包含参数。她是简单计算器的示例https://codepen.io/alchy/pen/xxOQqeO


慕娘9325324
浏览 103回答 1
1回答

扬帆大鱼

由于缺乏额外的信息,我想出了这个,它应该主要做你想做的事情,但是你需要决定你想要的 url 查询名称是什么,你希望如何/何时重置 url,如果你想要在值为 0 时显示查询值以及如何解析复选框值:$( document ).ready(function() {&nbsp; // Get the base URL&nbsp; const baseUrl = location.href.replace(location.search, '');&nbsp; // Find the Input elements&nbsp; const collectCheckbox = document.getElementById('collect');&nbsp; const platformInput = document.getElementById('nPlatform');&nbsp; const platformBudInput = document.getElementById('PlatformBud');&nbsp; const displayCheckbox = document.getElementById('display');&nbsp; const numStoresInput = document.getElementById('nStores');&nbsp; const storeBudInput = document.getElementById('storBud');&nbsp; // Attach listeners to the Input elements&nbsp; collectCheckbox.addEventListener('change', updateUrl);&nbsp; platformInput.addEventListener('input', updateUrl);&nbsp; platformBudInput.addEventListener('input', updateUrl);&nbsp; displayCheckbox.addEventListener('change', updateUrl);&nbsp; numStoresInput.addEventListener('input', updateUrl);&nbsp; storeBudInput.addEventListener('input', updateUrl);&nbsp; var timeout = null;&nbsp; function calculatePrice() {&nbsp; &nbsp; var collectCheck = document.getElementById('collect');&nbsp; &nbsp; var displayCheck = document.getElementById('display');&nbsp; &nbsp; var xPlatfor = document.getElementById('nPlatform').value;&nbsp; &nbsp; var pBudget = document.getElementById('PlatformBud').value;&nbsp; &nbsp; var xStore = document.getElementById('nStores').value;&nbsp; &nbsp; var sBudget = document.getElementById('storBud').value;&nbsp; &nbsp; var collectAmount,&nbsp; &nbsp; &nbsp; collectBudget,&nbsp; &nbsp; &nbsp; displayAmount,&nbsp; &nbsp; &nbsp; displayBudget,&nbsp; &nbsp; &nbsp; pricePerMonth,&nbsp; &nbsp; &nbsp; pricePerYear;&nbsp; &nbsp; collectAmount = 0;&nbsp; &nbsp; collectBudget = 0;&nbsp; &nbsp; displayAmount = 0;&nbsp; &nbsp; displayBudget = 0;&nbsp; &nbsp; pricePerMonth = 0;&nbsp; &nbsp; pricePerYear = 0;&nbsp; &nbsp; if (collectCheck.checked == true && xPlatfor > 0 && pBudget > 0) {&nbsp; &nbsp; &nbsp; collectAmount = pBudget;&nbsp; &nbsp; }&nbsp; &nbsp; if (displayCheck.checked == true && xStore > 0 && sBudget > 0) {&nbsp; &nbsp; &nbsp; displayAmount = xStore * sBudget;&nbsp; &nbsp; }&nbsp; &nbsp; pricePerMonth = +collectAmount + +displayAmount;&nbsp; &nbsp; pricePerYear = pricePerMonth * 12;&nbsp; &nbsp; document.getElementById('pricePerM').innerHTML = pricePerMonth;&nbsp; &nbsp; document.getElementById('pricePerY').innerHTML = pricePerYear;&nbsp; }&nbsp; function updateUrl() {&nbsp; &nbsp; if (!(collectCheckbox.checked || displayCheckbox.checked || platformInput.value || platformBudInput.value || numStoresInput.value || storeBudInput.value)) {&nbsp; &nbsp; &nbsp; return baseUrl;&nbsp; &nbsp; }&nbsp; &nbsp; let newUrl = new URL(`${baseUrl}?`);&nbsp; &nbsp; collectCheckbox.checked && newUrl.searchParams.append('collect', 1);&nbsp; &nbsp; displayCheckbox.checked && newUrl.searchParams.append('display', 1);&nbsp; &nbsp; platformInput.value && newUrl.searchParams.append('platforms', platformInput.value);&nbsp; &nbsp; platformBudInput.value && newUrl.searchParams.append('platformBudget', platformBudInput.value);&nbsp; &nbsp; numStoresInput.value && newUrl.searchParams.append('numStores', numStoresInput.value);&nbsp; &nbsp; storeBudInput.value && newUrl.searchParams.append('storeBud', storeBudInput.value);&nbsp; &nbsp; history.pushState({}, '', newUrl.toString());&nbsp; }&nbsp; calculatePrice();&nbsp; document.addEventListener('click', function (e) {&nbsp; &nbsp; if (e.target.name == 'question_1') {&nbsp; &nbsp; &nbsp; calculatePrice();&nbsp; &nbsp; }&nbsp; });});如有任何疑问,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript