duanbei6427 2015-02-08 09:16
浏览 55

使用PHP添加输入onclick jQuery

I am trying to write a script that will add inputs on click with jQuery/javascript.

But it doesn't work and I cann't find out why. Even console.log() doesn't show anything.

I am no master at jQuery, but it should work I think.

Here is my code

        <div id="wrap_inputs">
        <?php for($i = 1; $i <= $pocet; $i++) { ?>
          <div class="span2" id="span_group<?= $i; ?>">
            <label class="control-label" style="display: inline-block; font-size: 11px;"> <?= LANG_CAN_BE_LEFT_OUT; ?></label>
            <input type="checkbox" name="can_be_left_out[<?= $i; ?>]">
            <input class="span2 m-wrap" name="word[<?= $i; ?>]" type="text" placeholder="<?= LANG_WORD; ?> <?= $i; ?>" style="border: 1px solid black;">
            <input class="span2 m-wrap" name="replace[<?= $i; ?>][1]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
            <input class="span2 m-wrap" name="replace[<?= $i; ?>][2]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
            <input class="span2 m-wrap" name="replace[<?= $i; ?>][3]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
            <input class="span2 m-wrap" name="replace[<?= $i; ?>][4]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
            <input class="span2 m-wrap" name="replace[<?= $i; ?>][5]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
          </div>
        <?php } ?>
        </div>

Here is my Javascript code

    $(document).ready(function(){

  var counter = <?= $pocet; ?>;

  $("#addButton").click(function () { 

      var span2_group = $(document.createElement('div')).attr("id", 'span_group' + counter);

      span2_group.after().html('<label class="control-label" style="display: inline-block; font-size: 11px;"><?= LANG_CAN_BE_LEFT_OUT; ?>' +
        '<input type="checkbox" name="can_be_left_out[' + counter + ']">' +
        '<input class="span2 m-wrap" name="word[' + counter + ']" type="text" placeholder="<?= LANG_WORD; ?> ' + counter + '" style="border: 1px solid black;">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][1]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][2]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][3]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][4]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][5]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">'
      );

      span2_group.appendTo("#wrap_inputs");


      counter++;
  });

    $("#removeButton").click(function () {
    if(counter==1){
      alert("No more textbox to remove");
      return false;
    }   

    counter--;

    $("#span_group" + counter).remove();

});

Variable $pocet is number of inputs from first step sent via $_POST[].

  • 写回答

1条回答 默认 最新

  • dongyin2885 2015-02-08 09:37
    关注

    .....

      var span2_group = $(document.createElement('div')).attr("id", 'span_group' + counter);
    
      // PUT IT IN THE DOM HERE
      span2_group.appendTo("#wrap_inputs");
    
      // NOW it is in the DOM, you can call after()
      span2_group.after().html('<label class="control-label" style="display: inline-block; font-size: 11px;"><?= LANG_CAN_BE_LEFT_OUT; ?>' +
        '<input type="checkbox" name="can_be_left_out[' + counter + ']">' +
        '<input class="span2 m-wrap" name="word[' + counter + ']" type="text" placeholder="<?= LANG_WORD; ?> ' + counter + '" style="border: 1px solid black;">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][1]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][2]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][3]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][4]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
        '<input class="span2 m-wrap" name="replace[' + counter + '][5]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">'
      );
    

    ......

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题