【问题标题】:Prepopulate text field in a JQUERY with a URL parameter value使用 URL 参数值预填充 JQUERY 中的文本字段
【发布时间】:2020-08-04 18:48:20
【问题描述】:

我想在代码的 jquery sn-p 中使用 URL 参数值预先填充文本框(并将其变灰以进行编辑):

Jquery 代码:

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="email">\
                <input class="form-control" type="text" name="text" placeholder="@domain">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
  

我要预填充的特定文本字段:

<input class="form-control" type="text" name="domain" placeholder="@domain">\

获取上述参数值的函数:

function GetURLParameter(sParam)

    {

        var sPageURL = window.location.search.substring(1);

        var sURLVariables = sPageURL.split('&');

    for (var i = 0; i < sURLVariables.length; i++)

    {

        var sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] == sParam)

        {

            return sParameterName[1];

        }

    }

}​

【问题讨论】:

  • 我没有看到特定问题或特定问题的描述。您能否澄清一下究竟是什么没有按您的预期工作?
  • 我需要将“域”字段的值设置为 URL 参数值,换句话说就是 URL:test.com/?domain=mydomain 我需要知道如何在 JQUERY 的第一个 sn-p 中进行预填充代码
  • 我使用了以下值="${domain}" 但文本框只包含完全相同的文本并且不评估“域”的值
  • 字符串表达式的语法不正确。你想要,value =`${domain}`。但不确定你不会只写,value = domain 如果domain 是一个字符串。但我需要了解更多背景信息。

标签: javascript jquery


【解决方案1】:
$('#selector').val('value you want to input here')

https://api.jquery.com/val/

【讨论】:

    【解决方案2】:

    事实证明,解决方案是在 JQUERY 中编码的 html sn-p 上使用反引号,如下所示:

            $(".inc").append(`<div class="controls">\
                    <input class="form-control" type="text" id="email" name="email" placeholder="email">\
                    <input class="form-control" type="text" id="domain" name="domain"  value="${domain}">\
                    <a href="#" class="remove_this btn btn-danger">remove</a>\
                    <br>\
                    <br>\
                </div>`
               
                );
    

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      相关资源
      最近更新 更多