- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 jQuery 获取下拉菜单中所选项目的值,并使用该值替换其 href
中包含该字符串的所有 anchor 链接的特定部分。值(value)。我设置了一个 CodePen here ,我还在下面包含了相关的标记和 JS。
基本上,如果选择<option>
的值在下拉菜单中是“handle1”,例如,我想遍历具有某个类的父元素的所有链接,并将“SelectedHandle”占位符文本替换为该值。
$(document).ready(function() {
// define vars
var dropDown = $("#choices"),
grid = $('.row-grid');
// get value (handle) of selected item
dropDown.on('change', function() {
var handle = $(this).val();
var links = $('.animate-this a');
links.each(function() {
this.href = this.href.replace('SelectedHandle', handle);
});
// show divs upon making a selection
if (handle !== 0) {
grid.removeClass('hide-by-default');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- EXAMPLE SELECT OPTIONS BELOW -->
<select id="choices">
<option value="0" selected disabled hidden>Choose Below</option>
<option value="handle1">Person 1</option>
<option value="handle2">Person 2</option>
<option value="handle3">Person 3</option>
</select>
<!-- EXAMPLE DIVS BELOW -->
<div class="row row-grid hide-by-default">
<div class="col-6 col-sm-4 col-md-3">
<div data-animate-hover="2">
<div class="animate-this">
<a href="https://twitter.com/home?status=Hey %40SelectedHandle Message 1" target="_blank"><img src="https://via.placeholder.com/160x80" class="img-fluid rounded shadow">
</a>
</div>
</div>
</div>
<div class="col-6 col-sm-4 col-md-3">
<div data-animate-hover="2">
<div class="animate-this">
<a href="https://twitter.com/home?status=Hey %40SelectedHandle Message 2" target="_blank"><img src="https://via.placeholder.com/160x80" class="img-fluid rounded shadow">
</a>
</div>
</div>
</div>
</div>
我的问题是 change
功能仅替换所选第一个选项的链接中的文本;之后点击其他人将不会更新 hrefs。因此,如果您最初选择“Person 2”,您会看到链接更新以反射(reflect)该选项值,但随后选择“Person 1”或“Person 3”不会对 href 产生任何影响。我想知道这是否是与使用 this
有关的范围问题?感谢您在这里提供的任何帮助!
最佳答案
当您第一次更改选择下拉列表中的句柄时,href 中的“SelectedHandle”字符串将被替换为“handle1”或“handle2”等字符串。
因此,当您第二次或第三次更改 select 下拉列表中的 handle 时,href 中将不会有“SelectedHandle”字符串被替换。因此 href 不会改变。
我建议对 URL(href 标签)内的空格进行编码,因为某些浏览器会将 URL 中的空格转换为“%20”,而其他浏览器会将其转换为“+”。因此,将 href 标签内的空格替换为“%20”;
我对您的代码进行了 2 处更改。
尝试(运行)下面的代码片段。我相信它会按您的预期工作。
$(document).ready(function() {
// define vars
var dropDown = $("#choices"),
grid = $('.row-grid');
// get value (handle) of selected item
dropDown.on('change', function() {
var handle = $(this).val();
var links = $('.animate-this a');
//Replacing handle inside href
links.each(function() {
$trim_beginning = (this.href).substr(
(this.href).indexOf("%40") + 3
);
$trim_ending = $trim_beginning.substr(
0, $trim_beginning.indexOf("%20")
);
$replace_string = $trim_ending;
this.href = this.href.replace($replace_string, handle);
});
// show divs upon making a selection
if (handle !== 0) {
grid.removeClass('hide-by-default');
}
});
});
div,
img {
display: block;
padding: 0;
margin: 0;
}
select {
margin-bottom: 25px;
}
.hide-by-default {
display: none;
}
.col-6 {
margin-bottom: 25px;
}
<!--JQUERY CDN-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- EXAMPLE SELECT OPTIONS BELOW -->
<select id="choices">
<option value="0" selected disabled hidden>Choose Below</option>
<option value="handle1">Person 1</option>
<option value="handle2">Person 2</option>
<option value="handle3">Person 3</option>
</select>
<!-- EXAMPLE DIVS BELOW -->
<div class="row row-grid hide-by-default">
<div class="col-6 col-sm-4 col-md-3">
<div data-animate-hover="2">
<div class="animate-this">
<a href="https://twitter.com/home?status=Hey%20%40SelectedHandle Message%201" target="_blank"><img src="https://via.placeholder.com/160x80" class="img-fluid rounded shadow">
</a>
</div>
</div>
</div>
<div class="col-6 col-sm-4 col-md-3">
<div data-animate-hover="2">
<div class="animate-this">
<a href="https://twitter.com/home?status=Hey%20%40SelectedHandle%20Message%202" target="_blank"><img src="https://via.placeholder.com/160x80" class="img-fluid rounded shadow">
</a>
</div>
</div>
</div>
</div>
关于javascript - JS : Issue getting values from drop-down menu to replace parts of HREF strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56696759/
这确实是一个分开的用法问题,但欢迎提出有关如何实现此目的的其他想法。 我需要创建一个引导设备,设置如下: ( up to 4MB erase block size (EBS)): partitions
我想知道定义库及其内容的方式背后的原因是什么。更具体地说,库需要列出所有部分,并且这些部分需要说明它们所属的库。 这种双向对我来说似乎是不必要的,我希望从库中引用这些部件就足够了。此外,在库中添加或删
我对 lambda 表达式比较陌生,如果有人能解释为什么返回类型与我预期的不同,我会很高兴。 () -> MultipartBody.Part! 究竟是什么? Single.just{ val
我对 map reduce 输出部分文件有一些疑问。 1> part-r-*文件和map reduce输出的part-*文件有什么区别? part-r-* 是 mapper 的输出,part-* 是
我需要为移动应用程序创建 Rails API。 API 将发送和接收 json 格式的请求。 我还想创建一个后端来管理应用程序。后端将在桌面(管理员)上使用带有用户界面的 Rails。 我想知道是否有
我正在使用此功能发布多张图片 @Multipart @POST("addad") Call addad( @Part List files , @Part Multip
我有一个正在为项目开发的小型 Java 程序,它使用 JavaMail 从指定的 URI 中提取用户的收件箱,然后开始处理消息。 在 Outlook 中,属性菜单中有一个功能可以设置邮件的到期日期,它
我不完全理解 part 之间的区别/part of和 import/export在 Dart 中使用库时。例如: one.dart: library one; part "two.dart"; Cla
这个问题在这里已经有了答案: How can I upload files to a server using JSP/Servlet? (14 个答案) 关闭 6 年前。 我正在尝试通过 JSP
我想使用 spark 从 html 表单上传文件。以下是我处理发布路由的 java 函数: Spark.post("/upload", "multipart/form-data", (request,
对于我的 python 扩展,我有 C(来自嵌入式库)和 C++ 文件,它们被编译并链接在一起。只有 C++ 部分与 Python 接口(interface)(通过 SWIG)。这在 VS2015 的
我想使用子例程 sum_real 访问数组派生类型中数组的元素。即:对所有人的权重中的第一个条目求和。 type my_type real, dimension(:), allocatable
最近我将 sonarqube 从 4.0 升级到 4.3.3。升级后当我尝试运行 maven 构建时出现错误: Failed to execute goal org.codehaus.mojo:son
我拿了一个 wsp 文件,并像往常一样做了我的 stsadm -o addsolution。然后我进入中央管理->解决方案管理,结果一切正常。然后我部署了 Web 部件,到目前为止没有问题。 问题是当
在我的 Eclipse 插件中我有这个工作流: 在Package Explorer中获取当前选中的项目 做某事 在Package Explorer中获取当前选中的项目(同1) 做一些不同的事情 1(和
我正在尝试使用 Spring MVC 和 Thymeleaf 上传文件,但出现异常,提示未提供多部分配置。 这是我的 Thymeleaf 表格: Picture
我知道 map reduce 输出存储在名为 part-r-* for reducer 和 part-m-* for mapper 的文件中。 当我运行 mapreduce 作业时,有时会在单个文件中
我们在运行 CentOS 的 Virtual Box 上使用 Hadoop 的同时处理 BigData。每当我们执行某些程序时,它都会创建 2 个不同的文件 1) part-r-00000 和 2)
我在其他论坛上看到过有关此问题的相关帖子(请参阅: http://www.mrexcel.com/forum/showthread.php?t=372534 ),但我尝试了所有发现的方法,但没有成功。
我无法将图像上传到服务器。我在这里尝试了很多相关问题的解决方案,但没有一个对我有用。这是我的文件: SecurityApplicationInitializer: package co
我是一名优秀的程序员,十分优秀!