我有一组由另一个系统生成的锚。
锚是切换锚,因此是/否开关。
我需要为此添加用户输入的自定义文本。我以为我可以使用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>
<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>
$( 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
据我所知,href
不是setter方法,而只是吸气剂。您可以这样在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);