如何用模态文本框填充我的文本框

时间:2016-04-18 14:43:24

标签: javascript jquery twitter-bootstrap

我遇到了问题。我想取我的模态文本框的值,并在验证打印它在我的页面文本框,但我得到了这个 "[object Object]"

这是我的代码:

@Html.LabelFor(m => m.X.Y, new { @class = "col-sm-d col-sm-2 control-label" })
<div class="col-sm-2">
  @Html.TextBoxFor(m => m.X.Y, new { @class = "form-control", @id = "X_Y"})
</div>
<div class="col-sm-2">
  @Html.EnumDropDownListFor(m => m.X.Y, new { @class = "form-control", @id = "Dropdown1" })
</div>

@Html.LabelFor(m => m.X.Y, new { @class = "col-sm-d col-sm-2 control-label" })
<div class="col-sm-2">
  @Html.TextBoxFor(m => m.X.Y, new { @class = "form-control"})
</div>
<div class="col-sm-2">
  @Html.EnumDropDownListFor(m => m.X.Y, new { @class = "form-control", @id = "Dropdown2" })
</div>

我的模态:

<div id="myModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        Multiples
      </div>
      <div class="modal-body">
        <input type="text" id="test" />
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" data-dismiss="modal">
          Annuler
        </button>

        <button type="button" class="btn btn-success" data-dismiss="modal">
          Valider
        </button>

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

我的剧本:

$(function() {
  $("#Dropdown1").change(function() {
    //Recupere la value de la liste
    selection = $(this).val();
    //Mettre la valeur du choix de liste.
    if (selection == 10) {
      //Affiche le modal
      $('#myModal').modal('show');
    }
  });
});

var modalValue = $('#test');

$('#myModal').on('hidden.bs.modal', function() {
  $('#X_Y').val(modalValue);
});

1 个答案:

答案 0 :(得分:2)

要在文本框中获取值,您需要访问input元素的val()方法。 所以你应该这样做:

var modalValue = $('#test');
$('#myModal').on('hidden.bs.modal', function() {
  $('#X_Y').val(modalValue.val());
});
相关问题