从另一个页面选择的页面 Link 预填充复选框或单选字段?
Pre-Fill a Checkbox or Radio Field from a Page Link Selected from Another Page?
所以我一直在尝试弄清楚如何 auto-fill/select 从指定的 link 另一个页面上的复选框或单选框。
到目前为止,我已经弄清楚如何在单击表单页面上的定向 link 时 select 某些输入字段,其中包含以下内容 function/HTML:
脚本:
以及 HTML(在表单页面本身):
<a href='' id='select-checkbox1'>Click to Select</a>
有没有办法通过页面加载(在表单页面上)自动激活这样的功能,最初由另一个页面上的 link 提示?
然后我也在想,当与 URL 中调用的特定哈希 target/anchor 相对应时,也许可以自动填充一个字段。有什么想法吗?
非常感谢您的帮助,因为我在 Javascript/JQuery...
方面的专业知识有限
谢谢。
您可以使用这个问题的解决方案:How can I get query string values in JavaScript?
例如像这样:
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$('#checkbox1).prop('checked', getParameterByName('checkbox1'))
另一页的link可能是这样的:
<a href='pagewithcheckbox.html?checkbox1=checked' id='select-checkbox1'>Click to Select</a>
可以使用哈希将信息从页面 A 传送到页面 B。
也就是说,如果您不想使用服务器端资源。
A页:
<a href='pageB.html#myHash' id='select-checkbox1'>Click to Select</a>
B页:
$(document).ready(function(){
var hash = location.hash; // Here, it would be "#myHash"... So including the #
if(hash=="#myHash"){
$("#checkbox1").prop("checked", !$("#checkbox1").prop("checked") ); // that toggles the checked state
}
});
所以我一直在尝试弄清楚如何 auto-fill/select 从指定的 link 另一个页面上的复选框或单选框。
到目前为止,我已经弄清楚如何在单击表单页面上的定向 link 时 select 某些输入字段,其中包含以下内容 function/HTML:
脚本:
以及 HTML(在表单页面本身):
<a href='' id='select-checkbox1'>Click to Select</a>
有没有办法通过页面加载(在表单页面上)自动激活这样的功能,最初由另一个页面上的 link 提示?
然后我也在想,当与 URL 中调用的特定哈希 target/anchor 相对应时,也许可以自动填充一个字段。有什么想法吗?
非常感谢您的帮助,因为我在 Javascript/JQuery...
方面的专业知识有限谢谢。
您可以使用这个问题的解决方案:How can I get query string values in JavaScript?
例如像这样:
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$('#checkbox1).prop('checked', getParameterByName('checkbox1'))
另一页的link可能是这样的:
<a href='pagewithcheckbox.html?checkbox1=checked' id='select-checkbox1'>Click to Select</a>
可以使用哈希将信息从页面 A 传送到页面 B。
也就是说,如果您不想使用服务器端资源。
A页:
<a href='pageB.html#myHash' id='select-checkbox1'>Click to Select</a>
B页:
$(document).ready(function(){
var hash = location.hash; // Here, it would be "#myHash"... So including the #
if(hash=="#myHash"){
$("#checkbox1").prop("checked", !$("#checkbox1").prop("checked") ); // that toggles the checked state
}
});