【问题标题】:Pre-Fill a Checkbox or Radio Field from a Page Link Selected from Another Page?从另一个页面选择的页面链接预填充复选框或单选字段?
【发布时间】:2018-01-18 21:27:12
【问题描述】:

因此,我一直在尝试弄清楚如何从另一个页面上的指定链接中自动填充/选择复选框或单选字段,这一经历令人不快。

到目前为止,我已经弄清楚了如何在使用以下函数/HTML 单击表单页面上的定向链接时选择某些输入字段:

脚本:

还有 HTML(在表单页面本身上):

<a href='' id='select-checkbox1'>Click to Select</a>

有没有办法通过页面加载(在表单的页面上)自动激活这样的功能,最初由另一个页面上的链接提示?

然后我还在想,当与 URL 中调用的特定哈希目标/锚相对应时,可能会自动填充一个字段。有什么想法吗?

非常感谢您的帮助,因为我对 Javascript/JQuery 的专业知识有限...

谢谢。

【问题讨论】:

    标签: jquery checkbox field radio autofill


    【解决方案1】:

    可以使用哈希将信息从页面 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
      }
    });
    

    【讨论】:

    • 太棒了!这完全像一个魅力。非常感谢您的帮助!
    【解决方案2】:

    你可以使用这个问题的解决方案: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'))
    

    另一个页面上的链接可能是这样的:

    <a href='pagewithcheckbox.html?checkbox1=checked' id='select-checkbox1'>Click to Select</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      相关资源
      最近更新 更多