我想知道如何将多个值传递到查询字符串:
<label class="btn active">
<input
type="radio"
name="location"
id="us"
value="us"
checked
/>
US
</label>
上面的代码正确传递"us"
给查询字符串。
<label class="btn">
<input type="radio" name="location" id="uk" value="uk" />
UK
</label>
单选按钮正在传递:
$(document).ready(function() {
$("#submitButton").click(() => {
const val = $("input[name=location]:checked").value();
});
});
但是,如果我想同时传递两个参数,那么解决方案是什么?
您可以从无线电中检索值,然后使用split()方法将其分成两个值,如下所示:
const inputVal = document.querySelector('#both').value;
let countries = inputVal.split("&");
console.log(countries);
<label class="btn">
<input type="radio" name="location" id="both" value="us&uk" />Both
</label>
使用当前使用的jQuery方法,它看起来像这样:
$(document).ready(function() {
$("#submitButton").click(() => {
const val = $("input[name=location]:checked").value();
const bothVals = val.split("&"); //bothVals consists of two items "uk" and "us" now
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句