【问题标题】:Trying to use URL to complete a form with JQuery/Javascript尝试使用 URL 用 JQuery/Javascript 完成表单
【发布时间】:2020-05-21 16:53:50
【问题描述】:

我们刚刚有了一个新的 CRM 系统来管理我们的在线表格,我可以使用 JQuery 来操作它来完成额外的功能。

这是我们在当前网站上正在做的事情,我可以使用路径名来完成表单的各个部分。

例如...<URL>/John%20Smith/50 将在name 下预加载John Smithdonation£50 的表单。

这是我当前的新系统代码:

<script>
window.addEventListener( 'load', function() {
    var pageName = $(location).attr('pathname').split('/');
    var arr = $.makeArray(pageName);    
    $('input[name=field[91]]').eq(0).val(arr);
    $('input[name=field[9]]').eq(1).val(arr);
});
</script>

【问题讨论】:

  • 您正在分享一些发现,或者您有一些实际问题需要解决?还有……为什么&lt;script&gt;&lt;style&gt;里面?
  • 看这个小提琴:jsfiddle.net/9s3eugc8

标签: javascript jquery input parameters


【解决方案1】:

我在这里发布它作为答案:

// suppose window.location = "https://<your url>/John%20Smith/50"
var url = "https://<your url>/John%20Smith/50";

$(document).ready(function(){

    // $(location).attr('pathname').split('/')
    // replace this with the above code
    var pageName = url.split('/').splice(3);
    var arr = $.makeArray(pageName);    

    // if name is unique, you don't need .eq
    // you may not need decodeURIComponent if url is form window.location
    $('input[name="field[91]"]').val(decodeURIComponent(arr[0]));
    $('input[name="field[9]"]').val('£' + decodeURIComponent(arr[1]));

});

见小提琴:https://jsfiddle.net/9s3eugc8/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 2014-08-04
    • 2011-02-02
    相关资源
    最近更新 更多