【问题标题】:Add dynamic text to anchor link URL query为锚链接 URL 查询添加动态文本
【发布时间】:2020-01-29 01:55:27
【问题描述】:

我有一组由另一个系统生成的锚点。

锚点是切换锚点,真/假开关也是如此。

我需要添加用户输入的自定义文本。我想我可以使用 JQuery 来构建它,但系统根本没有按预期响应和更新。

我进行了广泛的搜索,但似乎找不到类似的问题;除了this question about dynamically adding get parameter to url on form submit。但那是表单形式,这应该用于锚点。

例如:

目前:

<table id='custRefTable'>
<tr>
    <td class="passed" title='Sunday, January  5, 2020 (date has passed)'><a href='toggle.php?date=2020-01-05'>05</td>
    <td class="passed" title='Monday, January  6, 2020 (date has passed)'><a href='toggle.php?date=2020-01-06'>06</td>
    <td class="passed" title='Tuesday, January  7, 2020 (date has passed)'><a href='toggle.php?date=2020-01-07'>07</td>
    <td class="passed" title='Wednesday, January  8, 2020 (date has passed)'><a href='toggle.php?date=2020-01-08'>08</td>
    <td class="passed" title='Thursday, January  9, 2020 (date has passed)'><a href='toggle.php?date=2020-01-09'>09</td>
    <td class="passed" title='Friday, January 10, 2020 (date has passed)'><a href='toggle.php?date=2020-01-10'>10</td>
    <td class="passed" title='Saturday, January 11, 2020 (date has passed)'><a href='toggle.php?date=2020-01-11'>11</td>
</tr>
</table>

我想用这个 HTML 添加一个 INPUT:

    <div>
        <p>Reference will be saved with each new booking made on this page.</p>
        <label for='customerRef'>Customer Reference:</label>
        <input type='text' name='customerRef' maxlength='150' id='customerRef' value='' placeholder='Reference details'>
        <input type='button' id='cset' value='Set'>
    </div>

还有我现在的 JS:

$( document ).ready(function() {
    $("#cset").click(function () {
        var lister = $("#custRefTable").find("a");
        var customerData = $("#customerRef").value;
        customerData = encodeURIComponent(customerData);
        $(lister).each(function () {
            $(this).href = $(this).href + "&custNote=" + customerData;
        });
    });

});

我没有收到控制台错误,但锚点也没有更新;参考 ID cset

意图是:

用户输入文本;按下按钮输入,文本值被转换为安全的 URL,并附加到上表中的每个锚点。

例如,在文本机器人中,我写“大树”并按“设置”;然后当点击表格中的任何锚点时;它转到:

  toggle.php?date=2020-01-10&custNote=great%20trees

我错过了什么或做错了什么?!

【问题讨论】:

  • $(this).attr('href', $(this).attr('href') + "&amp;custNote=" + customerData); 呢?
  • 查看this answer :-)

标签: javascript jquery html anchor


【解决方案1】:

据我所知,href 不是一个 setter 方法,而只是一个 getter。你可以在 jQuery 中使用 attr 这样做:

$(this).attr('href', $(this).attr('href') + "&custNote=" + customerData);

或者干脆省略 jquery 包装器并使用setAttribute

this.setAttribute(this.href, this.href + "&custNote=" + customerData);

如何防止我的代码添加多个 &custNote= 值?如果我单击“设置”两次,则值会添加两次:

toggle.php?date=2020-01-22&custNote=Horses and cheese&custNote=fielding

为此,您可以 split 符号上的 href 值,获取这个新形成的数组的第一个索引,然后像以前一样附加其余的:

$(this).attr('href', $(this).attr('href').split('&')[0] + "&custNote=" + customerData);

【讨论】:

  • 太棒了,这就是我想要的。我知道我在某个地方犯了一个愚蠢的错误!谢谢!
  • 还有一件事,你必须转换 var customerData = $("#customerRef").value;到 var customerData = $("#customerRef").val();.
  • 一个快速的辅助 - 我如何防止我的代码添加多个 &amp;custNote= 值?如果我单击“设置”两次,则会添加两次值:toggle.php?date=2020-01-22&amp;custNote=Horses and cheese&amp;custNote=fielding ?
  • @CarlEdwards 差不多,但 date 原始值 toggle.php?date=2020-01-11 被覆盖并消失或变为“未定义”
  • 这太棒了,很有魅力——谢谢你的帮助,卡尔。 :-)
猜你喜欢
  • 1970-01-01
  • 2017-06-16
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2013-03-05
  • 2014-12-20
  • 1970-01-01
相关资源
最近更新 更多