想知道为什么我的JQuery数组循环没有设置我的href's正确
Wondering why my JQuery array loop is not setting my href's correctly
所以让我给你一个我在做什么的想法。这是我正在做的一些家庭作业,现在已经被卡住了。我正在创建一个页面,您可以从一堆不同的复选框中进行选择。根据复选框的不同,你可以点击我编程的"显示资源"按钮,打开一个带有相应复选框的新窗口,显示它们的值,它们的值也是这些特定资源的URL。听起来很简单,对吧?这是事物的基本结构。
//this is the main function that is making an array of the checked check boxes and displying them on the new window, problem is I am only able to set the href attibute to only the first array item...
$(function() {
$(":checkbox").change(function() {
var arr = $(":checkbox:checked").map(function() { return $(this).val(); }).get();
$("#messagepop").html('<a id="resourceLink">' + arr.join('</a><br><a id="resourceLink">') + '</a>');
for (var i = 0; i < arr.length; i++){
$("#resourceLink").attr('href', arr[i]);
}
});
});
//these are functions for the new window
function deselect(e) {
$('.pop').slideFadeToggle(function() {
e.removeClass('selected');
});
}
$(function() {
$('#contact').on('click', function() {
if($(this).hasClass('selected')) {
deselect($(this));
} else {
$(this).addClass('selected');
$('.pop').slideFadeToggle();
}
return false;
});
$('.close').on('click', function() {
deselect($('#contact'));
return false;
});
});
$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};
.messagepop {
background-color:#FFFFFF;
border:1px solid #999999;
cursor:default;
display:none;
margin-top: 15px;
position:absolute;
text-align:left;
width:394px;
z-index:50;
padding: 25px 25px 20px;
}
label {
display: block;
margin-bottom: 3px;
padding-left: 15px;
text-indent: -15px;
}
.messagepop p, .messagepop.div {
border-bottom: 1px solid #EFEFEF;
margin: 8px 0;
padding-bottom: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide4">
<h1>Resources</h1>
<p>(Select the resources you wish to receive information about. Click on the "Show Resources" button when you are ready)</p>
<form action="">
<input type="checkbox" value="https://google.com/search?q=CareerPreparation">Career Preparation<br>
<input type="checkbox" value="https://google.com/search?q=CollegeAdvising">College Advising<br>
<input type="checkbox" value="https://www.google.com/search?q=financial+aid">Financial Aid<br>
<input type="checkbox" value="https://www.google.com/search?q=fitness+recreation">Fitness & Recreation<br>
<input type="checkbox" value="https://www.google.com/search?q=health+services">Health Services<br>
<input type="checkbox" value="https://www.google.com/search?q=housing">Housing<br>
<input type="checkbox" value="https://www.google.com/search?q=library">Library<br>
<input type="checkbox" value="https://www.google.com/search?q=parking">Parking<br>
<input type="checkbox" value="https://www.google.com/search?q=housing">Scholarships<br>
<input type="checkbox" value="https://www.google.com/search?q=student+employment">Student Employment<br>
<input type="checkbox" value="https://www.google.com/search?q=student+organization">Student Organizations<br>
<input type="checkbox" value="https://www.google.com/search?q=tutoring">Tutoring Support<br>
<input type="checkbox" value="https://www.google.com/search?q=writing">Writing Support<br>
</form>
<div class="messagepop pop">
<p><a class="close" href="/">Close</a></p>
<div id="messagepop" >
</div>
</div>
<a href="/contact" id="contact">Show Resources</a>
</div>
因此,我遇到的问题是,我无法正确设置新div窗口中显示的新<a>
标记的href属性。所有这些的关键是,我必须用选中的复选框制作一个数组,并让它们链接到相应的资源。
所以真正的问题是,我错过了什么?我可以看到数组有正确的元素链接,那么为什么它们没有显示在其他显示的元素上呢。希望这是有道理的,如果我在这里向你展示的内容令人困惑,或者我需要澄清,请让我知道,谢谢!
您正在为多个元素设置相同的ID,这就是SIN
$("#messagepop").html('<a class="resourceLink">' + arr.join('</a><br><a class="resourceLink">') + '</a>');
for (var i = 0; i < arr.length; i++){
$(".resourceLink").eq(i).attr('href', arr[i]);
}
根本不需要使用id,至少在显示的代码中不需要。
$(":checkbox").change(function() {
var messagepop = $("#messagepop");
// remove all child nodes
messagepop.empty();
// add the checked items as anchor tags
$(":checkbox:checked").each(function() {
var val = $(this).val();
$('<a />')
.attr("href", val)
.text(val)
.appendTo(messagepop);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide4">
<h1>Resources</h1>
<p>(Select the resources you wish to receive information about. Click on the "Show Resources" button when you are ready)</p>
<form action="">
<input type="checkbox" value="https://google.com/search?q=CareerPreparation">Career Preparation
<br>
<input type="checkbox" value="https://google.com/search?q=CollegeAdvising">College Advising
<br>
<input type="checkbox" value="https://www.google.com/search?q=financial+aid">Financial Aid
<br>
<input type="checkbox" value="https://www.google.com/search?q=fitness+recreation">Fitness & Recreation
<br>
<input type="checkbox" value="https://www.google.com/search?q=health+services">Health Services
<br>
<input type="checkbox" value="https://www.google.com/search?q=housing">Housing
<br>
<input type="checkbox" value="https://www.google.com/search?q=library">Library
<br>
<input type="checkbox" value="https://www.google.com/search?q=parking">Parking
<br>
<input type="checkbox" value="https://www.google.com/search?q=housing">Scholarships
<br>
<input type="checkbox" value="https://www.google.com/search?q=student+employment">Student Employment
<br>
<input type="checkbox" value="https://www.google.com/search?q=student+organization">Student Organizations
<br>
<input type="checkbox" value="https://www.google.com/search?q=tutoring">Tutoring Support
<br>
<input type="checkbox" value="https://www.google.com/search?q=writing">Writing Support
<br>
</form>
</div>
<div class="messagepop pop">
<div id="messagepop"></div>
</div>
相关文章:
- 希望在单击href链接时显示我的广告
- 如何使我的.selected类在hrefs列表中重新加载后保持在正确的a(href)上
- 我的 href 点击事件不起作用
- 在 HREF 中使用 IF,同时传递变量,并在单击 href 按钮时递增我的变量
- 我可以使用 javascript:this.child.src 作为链接的 href 吗?
- 单击它们时无法在ul中打开我的href
- 想知道为什么我的JQuery数组循环没有设置我的href's正确
- Jquery focus()和blur()对我的href不起作用.为什么
- AngularJS将我的ng-href "斜杠"到“% 2 f"
- 当我使用href返回到前一个页面时,页面的一部分会在绘制页面之前闪烁
- 将.html添加到我的href中
- 我怎么能得到与jQuery计数链接的href值
- 在我的例子中,一个href '
- Href在我的导航菜单栏列表中不起作用
- 谁能告诉我如何禁用这段代码中的href链接,而不会失去我的网站上的嵌入能力
- 如何在我的href = "Home/CompletedGrid发送数据?Value = ?????"
- 如何在我的
html中的Href路径 - JavaScript -当我更改链接的href时未定义链接
- 当我按下另一个href时,如何获取链接的href
- dojo contentPane,我张贴html代码与href在我的contentPane.html代码有一个脚本,但不