青衫唯衣 2017-05-24 18:55 采纳率: 17.4%
浏览 2442
已采纳

遍历出来的class相同 所以我怎么才能每个button对应text框

 @{ List<ExaminationHerp.BookTable> users = (List<ExaminationHerp.BookTable>)ViewBag.Users;
                  foreach (ExaminationHerp.BookTable item in users)
                {
        <div class="rightmaintopleft">


          <div class="rightmaintopcontent"> <a href="#"><img src="@item.Img" border="0" style="float:left";"width:41px;height:64px" /></a>
            <div style="float:left; width:154px">
              <div class="wen1">@item.BookName
                <div>
                  <div class="wen2">出版社:@item.Press</div>
                  <div class="wen3">价格¥@item.Money(7.1折)</div>

                  <div class="wen4"><img src="~/BooxCssJsImg/img/che1.png" border="0"/> <a href="#">购买</a> | <img src="~/BooxCssJsImg/img/da.png"/><a href="#">数量:</a>  <input type="button" value="+" class="input2" style="width:20px"/ ">  <input  type="text" style="width:20px;margin-left:140px"; class="input1" value="0"/> <input  class="input3" type="button" value="-" style="width:20px"/>  </div>

                </div>
              </div>
            </div>
          </div>   
        </div>



遍历出来的 button 当点击+时 text的值+1 点击-时-1 遍历出来的class相同 所以我怎么才能每个button对应text框

  • 写回答

5条回答 默认 最新

  • 战在春秋 2017-05-25 00:24
    关注

    和button对应的text框,应该同button在同一个父节点下。
    可以用如下代码实现题目需求:

    Btn.onclick = function() { 
        var children = this.parentElement.childNodes;
        var text;
        for(var i = 0; i < children.length; i ++) {
            if(children[i].nodeType === Node.TEXT_NODE) {
                text = children[i].nodeValue;
                break;
            }
        }
       alert(text); 
    }
    

    思路是得到button父节点下元素数组,循环遍历得到对应text框。

    用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢。

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

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算