Logica_ 2015-07-14 09:14 采纳率: 0%
浏览 1788
已采纳

jquery如何根据文本框中输入的数,实现数组循环

实现了点击添加选项增加行,但是每次增加一行都要用一个else if,所以在后面我增加了一个

 最多<input type="text" name="maxnum" style="width: 4%"/>项

用来输入本次添加选项需要设置多少项,超出就做出提示,做了很久还是做不出来,菜鸟求助各位

 <script>
 function del(t){
     $(t).parent('div').remove();
}
function add(){
     var lastInput = $('#contain input:last');
     var phName = lastInput.attr('placeholder');
     var maxNum = $("#maxnum").val();

     phName = phName.replace("选项","");
     if(phName=='1'){
          phName='2'
     }else if(phName=='2'){
          phName='3'
     }else if(phName=='3'){
          phName='4'
     }else if(phName=='4'){              **_  //这部分如何用数组循环实现_**
          phName='5'
     }else if(phName=='5'){
          phName='6'
     }
     $('<div class="inputs"><input input type="text" id="commentx" name="comment" maxlength="30" style="width: 80% "  placeholder="选项'+phName+'" /><span onclick=del(this)>删除</span></div>').insertAfter(lastInput.parent('div'));
}
</script>
 <tr id="tr">
        <td class="table_th" >题目选项:        
        <td width="" id="contain">
              <div class='inputs'>
              <input type="text" id="comment" name="comment"
            maxlength="30" style="width: 80%"  placeholder="选项1"/><font
          color="red"> *</font></div>

              <div class='inputs'><input type="text" id="title" name="title"
            maxlength="30" style="width: 80%"" placeholder="选项2" />&nbsp;</div>

              <div class='inputs'><input type="text" id="commentx" name="comment"
            maxlength="30" style="width: 80% " " placeholder="选项3" /><span  onclick=del(this)>删除</span></div>

              <div class='inputs'><input type="text" id="commentx" name="comment"
            maxlength="30" style="width: 80% "  placeholder="选项4" /><span  onclick=del(this)>删除</span></div>

            <div><a href="javascript:;" class="addRow" id="addRow" onclick="javascript:add()">+添加选项</a></div>

            </td>
    </tr>

        <tr>
            <td class="table_th">选择模式:</td>
            <td width="">
            最多<input type="text" id="maxnum" name="maxnum" style="width: 4%"/>项
            </td>
        </tr>
  • 写回答

6条回答 默认 最新

  • danielinbiti 2015-07-14 13:44
    关注
     <!DOCTYPE html> 
    <html>
    <head> 
        <script src="http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js"></script>
    
    </head> 
    <script>
        function del(t){
             $(t).parent('div').remove();
        }
        function add(){
             var val = $('#maxnum').val();
             if(!val){
                  alert('请填写最多项');
                  return;
             }
             var lastInput = $('#contain input:last');
             var phName = lastInput.attr('placeholder');
             phName = phName.replace("选项","");
             phName = parseInt(phName,10)+1;
             if(parseInt(val,10)>=parseInt(phName,10)){
                  $('<div class="inputs"><input input type="text" id="commentx" name="comment" maxlength="30" style="width: 80% "  placeholder="选项'+phName+'" /><span onclick=del(this)>删除</span></div>').insertAfter(lastInput.parent('div'));
             }else{
                 alert('超出了最大项');
             }
        }
    </script>
    <body> 
        最多<input type="text" id='maxnum' name="maxnum" style="width: 4%"/>项
        <table id="table_items" class="hovertable" style="width: 100%;" cellspacing="0"
        cellpadding="0">
        <tr id="tr">
            <td class="table_th" >题目选项:        
            <td width="" id="contain">
                  <div class='inputs'>
                  <input type="text" id="comment" name="comment"
                maxlength="30" style="width: 80%"  placeholder="选项1"/><font
              color="red"> *<span  onclick=del(this)>删除</span></font></div>
                <div><a href="javascript:;" class="addRow" id="addText" onclick="javascript:add()">+添加选项</a></div>
    
                </td>
        </tr>
        </table>
    </body> 
    </html> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组