douzhengnao8265 2016-01-14 20:40
浏览 24
已采纳

Javascript相当于php for loop效率

first time posting so I apologize if I miss something obvious here.

What I am trying to do is pretty simple....in php. I can do it in my sleep. However, my page requires an integration of javascript, which I am not particularly skilled in yet.

I have a question about a working portion of the script, that I would like to make more efficient.

Question: I have a series of checkboxes that a user will 'check' if they have done something. Javascript works just fine, but I need to use a loop so my brain doesn't hurt from all the unnecessary lines of code.

Here's the bulky ugly stuff:

$("#M1L1Box").click(function() {
  $("#M1L1BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L2Box").click(function() {
  $("#M1L2BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L3Box").click(function() {
  $("#M1L3BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L4Box").click(function() {
  $("#M1L4BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L5Box").click(function() {
  $("#M1L5BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L6Box").click(function() {
  $("#M1L6BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L7Box").click(function() {
  $("#M1L7BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

$("#M1L8Box").click(function() {
  $("#M1L8BoxFeedback").text(this.checked ? "- completed" : "- mark as complete");
});

And here's ONE version of what I have tried in my hours of wasting time to make this efficient:

  for(i=1; i<=8; i++){

  var checkBoxCode = "#M1L" + i +"Box";
  var feedbackCode = "#M1L" + i + "BoxFeedback";

  $(checkBoxCode).click(function() {
    $(feedbackCode).text(this.checked ? "- completed" : "- mark as complete");
  });
}

Sorry, here is the html:

<input type="checkbox" id="M1L1Box" class="checkbox1" value="M1L1">
<label for="M1L1Box" id="M1L1Label"> - Module 1 Lesson 1 <span id="M1L1BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L2Box" class="checkbox1" value="M1L2">
<label for="M1L2Box" id="M1L2Label"> - Module 1 Lesson 2 <span id="M1L2BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L3Box" class="checkbox1" value="M1L3">
<label for="M1L3Box" id="M1L3Label"> - Module 1 Lesson 3 <span id="M1L3BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L4Box" class="checkbox1" value="M1L4">
<label for="M1L4Box" id="M1L4Label"> - Module 1 Lesson 4 <span id="M1L4BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L5Box" class="checkbox1" value="M1L5">
<label for="M1L5Box" id="M1L5Label"> - Module 1 Lesson 5 <span id="M1L5BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L6Box" class="checkbox1" value="M1L6">
<label for="M1L6Box" id="M1L6Label"> - Module 1 Lesson 6 <span id="M1L6BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L7Box" class="checkbox1" value="M1L7">
<label for="M1L7Box" id="M1L7Label"> - Module 1 Lesson 7 <span id="M1L7BoxFeedback"></span></label><br>

<input type="checkbox" id="M1L8Box" class="checkbox1" value="M1L8">
<label for="M1L8Box" id="M1L8Label"> - Module 1 Lesson 8 <span id="M1L8BoxFeedback"></span></label><br>

Not sure why this isn't working, but am sure you js whizzes are slapping your forehead at how easy this is. I have tried multiple solutions found here and elsewhere, but never am able to successfully adapt examples to my code. Thank you so much for your help!!!

  • 写回答

3条回答 默认 最新

  • dpir3950 2016-01-14 20:51
    关注

    Why don't you just use a common class and use string concatenation to get the second element.

    $(".item").on("change", function() {
      $("#" + this.id + "Feedback").text(this.checked ? "- completed" : "- mark as complete");
    }).trigger("change");
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" id="M1L1Box" class="item" />
    <label id="M1L1BoxFeedback"></label>
    <br/>
    <input type="checkbox" id="M2L2Box" class="item" />
    <label id="M2L2BoxFeedback"></label>
    <br/>
    <input type="checkbox" id="M3L3Box" class="item" />
    <label id="M3L3BoxFeedback"></label>
    <br/>

    And depending on your HTML structure, you really do not even need JavaScript to change the text of an element linked to the checkbox.

    .item + label span { display: none }
    .item + label span + span { display: inline; }
    .item:checked + label span { display: inline; }
    .item:checked + label span + span { display: none; }
    <input type="checkbox" id="M1L1Box" class="item" />
    <label id="M1L1BoxFeedback"> - <span>completed</span><span>mark as complete</span></label>
    <br/>
    <input type="checkbox" id="M2L2Box" class="item" />
    <label id="M2L2BoxFeedback"> - <span>completed</span><span>mark as complete</span></label>
    <br/>
    <input type="checkbox" id="M3L3Box" class="item" />
    <label id="M3L3BoxFeedback"> - <span>completed</span><span>mark as complete</span></label>
    <br/>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。