简体   繁体   English

更改ID的复选框和标签

[英]Change checkbox and label for ID

I have a function that is replacing placeholders in a string of HTML with content from JS objects: 我有一个函数,用JS对象的内容替换HTML字符串中的占位符:

function inputCards() {
    $('#maincontent').empty();
    storyQuests.name.forEach(function(val, i) {
        var formattedCard = mainCardHTML.replace('%questName%', storyQuests.name[i])
                                        .replace('%questChapter%', storyQuests.chapter[i])
                                        .replace('%questImg%', storyQuests.img[i])
                                        .replace('%questDescription%', storyQuests.description[i])
                                        .replace('replaceBox', storyQuests.check[i]);
        $('#maincontent:last').append(formattedCard);
    })
}

This is how the code is now, the problem I ran into is that I am using materialize and the checkbox code on a card is: 现在是这样的代码,我遇到的问题是我正在使用物化,而卡上的复选框代码是:

<div class="card-action">
  <form action="#">
    <p>
      <input type="checkbox" id="replaceBox" />
      <label for="replaceBox" class="white-text">Complete!</label>
    </p>
  </form>
</div>

Everything on the page is going OK, except when it loads all the cards ALL of the checkboxes when clicked only effect the checkbox in the first card! 该页面上的所有内容都可以正常运行,除非当它加载所有卡时,单击时仅选中所有复选框中的所有复选框! So I assumed this was because they all had the same ID of "replaceBox". 所以我认为这是因为它们都具有相同的ID“ replaceBox”。 So I added this to solve it: 所以我添加了这个来解决它:

.replace('replaceBox', storyQuests.check[i]);

(Actually I started with something else that didn't work at all, tried several other ways and eventually gave up and created an additional item in my object to hold the checkbox IDs that that is referencing: (实际上,我从根本无法使用的其他东西开始,尝试了几种其他方法,最终放弃了,并在我的对象中创建了一个附加项来保存所引用的复选框ID:

check: ['box0', 'box1', 'box2', 'box3', 'box4', 'box5', 'box6', 'box7', 'box8', 'box9', 'box10', 'box11', 'box12', 'box13', 'box14', 'box15', 'box16', 'box17', 'box18', 'box19', 'box20', 'box21', 'box22', 'box23', 'box24', 'box25', 'box26', 'box27', 'box28', 'box29', 'box30', 'box31', 'box32', 'box33', 'box34', 'box35', 'box36', 'box37', 'box38', 'box39', 'box40', 'box41', 'box42']

However, even though the .replace works on everything else, on these IDs it doesn't seem to, it stops ALL checkboxes from working and the console shows this error: 但是,即使.replace可以在其他所有项目上使用,但在这些ID上却似乎不起作用,它会停止所有复选框的工作,并且控制台会显示此错误:

GET file:///A:/sites/mysite-com/undefined
net::ERR_FILE_NOT_FOUND

So ultimately, how do I replace the words "replaceBox" with different values, or honestly any other solution to allow each checkbox to be checked independently. 因此,最终,如何将“ replaceBox”一词替换为不同的值,或者说真的是任何其他解决方案以允许独立选中每个复选框。 I thought about removing any ID at all on the checkboxes but I feel I'll need them in the future as I plan on making a way for people to login and have it save the checked state of each card. 我曾考虑过删除复选框上的所有ID,但我觉得以后会需要它们,因为我计划为人们提供一种登录方法,并保存每个卡的已检查状态。

EDIT 1: The mainCardHTML code: 编辑1: mainCardHTML代码:

var mainCardHTML = "<div class='col hide-on-small-only m3'>&nbsp;</div>" +
                   "<div class='col s6'>" +
                     "<h5 class='header xvred-text'id='replaceMe'>%questName%</h5>" +
                     "<h6 class='header xvblue-text'id='replaceMe'>%questChapter%</h6>" +
                     "<div class='card horizontal hoverable'>" +
                       "<div class=card-image><img id='replaceMe' src=%questImg%></div>" +
                       "<div class='card-stacked xvred'>" +
                         "<div class='card-content xvblue'>" +
                           "<p id='replaceMe'>%questDescription%" +
                         "</div>" +
                         "<div class=card-action>" +
                           "<form action=#>" +
                             "<p><input id='replaceBox' type=checkbox>" +
                             "<label class=white-text for='replaceBox'>Complete!</label>" +
                           "</form>" +
                         "</div>" +
                       "</div>" +
                     "</div>" +
                   "</div>" +
                   "<div class='col hide-on-small-only m3'>&nbsp;</div>"

The storyQuests object(The actual object is way to long to post here for readability so I replaced the strings): storyQuests对象(为了便于阅读,实际对象很长时间就可以在此处发布,因此我替换了字符串):

var storyQuests = {
    name: ["43 strings", "43 strings", "..."],
    chapter: [43 numbers, 43 numbers, ...],
    img: ["43 strings", "43 strings", "..."],
    description: ["43 strings", "43 strings", "..."]
    check: ["box0", "box1", "..."]
}

First a little note, review the HTML code of mainCardHTML , there are two <p> elements unclosed (no </p> ) and some of the html attributes don't have the ' in their values. 首先,请注意一下mainCardHTML的HTML代码,其中有两个<p>元素未关闭(否</p> ),并且某些html属性的值中没有'

In your storyQuests object, you don't have the comma before the check field. 在您的storyQuests对象中,在check字段之前没有逗号。 It's probably a typo pasting the code in the question, but verify it. 将代码粘贴到问题中可能是一个错字,但请对其进行验证。

Javascript function replace by default only replace the first appearance of the word. 默认情况下,Javascript函数replace仅替换单词的第一个出现。 In your case, you have two replaceBox on each card, so you should use... 对于您的情况,每个卡上都有两个replaceBox ,因此应使用...

.replace(/replaceBox/g, storyQuests.check[i]

With all this, your replacement code works without problem in fiddle... 有了这些,替换代码就可以毫无问题地工作了。

https://fiddle.jshell.net/rigobauer/v8erbuur/ https://fiddle.jshell.net/rigobauer/v8erbuur/

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

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