通过AJAX和JQUERY添加输入表单

时间:2018-05-17 17:26:07

标签: javascript jquery ajax

我现在遇到的问题是,我现在想要添加的内容也只是在主JavaScript文件中创建它。

有没有办法可以将这些内容放在其他HTML文件中,然后在JavaScript文件中使用AJAX调用它,并将这些内容附加到循环中,以便将它从选择输入标记中选择的次数追加?

以下是选择输入代码:

<label for="beneficiarios">Cantidad de beneficiarios</label>
<select name="beneficiarios" id="beneficiarios">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

以下是我要添加的内容:

<div class="row beneficiario-info">
  <div class="col-md-6">
    <div class="form-group w-75">
      <label for="bene-nombreyapellido">Nombre y Apellido</label>
      <input type="text" name="bene-nombreyapellido" id="bene-nombreyapellido" class="form-control">
    </div>
    <!-- form group -->
    <div class="form-group w-75">
      <label for="bene-ci-numero">Cedula de identidad</label>
      <div class="input-group">
        <div class="input-group-prepend">
          <select name="bene-ci-tipo" id="bene-ci-tipo" class="custom-select">
            <option value="VE">V</option>
            <option value="EX">E</option>
          </select>
        </div>
        <!-- cedula tipo prepend -->
        <input type="text" name="bene-ci-numero" id="bene-ci-numero" class="form-control" maxlength="8">
      </div>
      <!-- input group -->
    </div>
    <!-- form group -->
  </div>
  <!-- col -->
  <div class="col-md-6">
    <div class="form-group w-75">
      <label for="parentezco">Parentezco</label>
      <select name="parentezco" id="parentezco" class="custom-select">
        <option value="">---------</option>
        <option value="hijo">Hijo</option>
        <option value="padre">Padre</option>
        <option value="hermano">Hermano o Hermana</option>
        <option value="conyugue">Conyugue</option>
      </select>
    </div>
    <div class="form-group">
      <label for="servicioadicional">Servicios Adicionales</label>
      <select name="servicios_beneficiario" class="custom-select" id="servicios_beneficiario" multiple="multiple">
        <option value="medico">Médico</option>
        <option value="odontologico">Odontológico</option>
        <option value="funerario">Funerario</option>
        <option value="vial">Víal</option>
      </select>
    </div>
  </div>
  <!-- col -->
</div>
<!-- row beneficiario -->

以下是JavaScript代码:

var number = 0;

$('#ctd-beneficiarios').on('change', function() {
  numero = $(this).val();
  beneficiarios_wrapper.html('');

  for (var i = 0; i < numero; i++) {

  }
}); 

beneficiarios_wrapper是我要将内容附加到的div。

1 个答案:

答案 0 :(得分:1)

不是将其存储在单独的文件中并通过AJAX检索,而是使用 像这样的隐藏模板:

&#13;
&#13;
$main = $('#main');
for(var i=0;i<5;i++){
  var $template = $('.template').clone();
  $template.removeClass('template');
  $template.find('.mainText').text("Template "+i);
  $main.append($template);
  $template.show();
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
  <div class="template" style="display:none;">
    <span class ="mainText"></span>
  </div>
</div>
&#13;
&#13;
&#13;

或者,正如J. Titus所建议的那样:

&#13;
&#13;
$main = $('#main');
for(var i=0;i<5;i++){
  var template = document.getElementById('template').innerHTML;
  var $template = $(template)
  $template.find('.mainText').text('Template '+i)
  $main.append($template);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="template" type="text/template">
    <div>
      <div class="test">
      <span class="mainText">test</span>
      </div>
    </div>
</script>
<div id="main">
</div>
&#13;
&#13;
&#13;