简体   繁体   English

将动态文本添加到锚链接 URL 查询

[英]Add dynamic text to anchor link URL query

I have a set of anchors which are generated by another system.我有一组由另一个系统生成的锚点。

The anchors are toggle anchors so are true/false switches.锚点是切换锚点,因此是真/假开关。

I need to add to this a custom text entered by the user.我需要向其中添加用户输入的自定义文本。 I imagined I could use JQuery to build this but the system is simply not responding and updating as expected.我想我可以使用 JQuery 来构建它,但系统根本没有按预期响应和更新。

I have searched extensively all over and I can't seem to find a similar issue;我已经到处搜索,但似乎找不到类似的问题; aside from this question about dynamically adding get parameter to url on form submit .除了这个关于在表单提交上动态添加 get 参数到 url 的问题 But that is form forms and this should be for anchors.但那是形式形式,这应该用于锚点。

For example:例如:

Currently:目前:

<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>

With this HTML I want to add an INPUT:使用此 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>

And my JS as I have at the moment:还有我现在的 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;
        });
    });

});

I get no console errors, but also the anchors are not being updated;我没有收到控制台错误,但也没有更新锚点; the reference id cset .参考 ID cset

The intention is:意图是:

user enters text;用户输入文本; the button input is pressed and the text value is converteed to URL safe and is appended to each anchor in the table above.按钮输入被按下,文本值被转换为 URL 安全并附加到上表中的每个锚点。

So for example in the text bot I write "great trees" and press "Set";例如,在文本机器人中,我写了“大树”并按“设置”; then when any anchor in the table is clicked;然后当点击表格中的任何锚点时; it goes to:它转到:

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

What am I missing or doing wrong?!我错过了什么或做错了什么?!

To my knowledge, href isn't a setter method but just a getter.据我所知, href不是一个 setter 方法,而只是一个 getter。 You could do it in jQuery using attr as so:你可以在 jQuery 中使用attr这样做:

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

or simply omit the jquery wrapper and use setAttribute或者干脆省略 jquery 包装器并使用setAttribute

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

How would I prevent my code from adding multiple &custNote= values?如何防止我的代码添加多个 &custNote= 值? If I click "Set" twice the value is added twice:如果我单击“设置”两次,则该值会添加两次:

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

For this you could split the href value on the ampersand, grab the first index of this newly formed array, and then append the rest like you did before:为此,您可以split & 符号上的 href 值,获取这个新形成的数组的第一个索引,然后像以前一样附加其余部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM