jQuery单击打开引导模态并更改选择选项

时间:2018-11-12 11:01:17

标签: javascript jquery html

我有一个带有选择输入元素的引导程序模态。单击按钮后,我需要打开模态并更改选择元素的选择。请参见下面的JS小提琴链接。我该如何工作?

JS Fiddle

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<button id="audi" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">Open modal with Audi Selected</button>

<button id="mercedes" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">Open modal with Mercedes Selected</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">

            <div class="modal-body">
                <select id="selection">
                    <option value="volvo">Volvo</option>
                    <option value="saab">Saab</option>
                    <option value="mercedes">Mercedes</option>
                    <option value="audi">Audi</option>
                </select>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

JS

this.$("#audi").click(function() {
    $("#myModal #selection option[value='file']").attr('selected', 'selected');
});

3 个答案:

答案 0 :(得分:1)

由于按钮的id与select option 值匹配,因此可以将this元素的 id 设置为select元素的值。

尝试以下方式:

$("#audi,#mercedes").click(function() {
  var idAsValue = $(this).attr('id');
  $("#selection").val(idAsValue);
});
.btn-info {
  margin-bottom: 50px;
}
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link rel="stylesheet" type="text/css" href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script type="text/javascript" src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<button id="audi" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">Open modal with Audi Selected</button>

<button id="mercedes" class="btn btn-primary btn-block" data-toggle="modal" data-target="#myModal">Open modal with Mercedes Selected</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      
      <div class="modal-body">
        <select id="selection">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
        </select>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

答案 1 :(得分:1)

尝试

$(".btn").click(function() {
     $('#selection').val($(this).attr('id'));
});

DEMO HERE

答案 2 :(得分:1)

尝试一下。

this.$(".btnCustom").click(function() {
var value = $(this).attr('id');
     $("#myModal #selection option[value="+value+"]").attr('selected', 'selected');
});
.btn-info {
    margin-bottom: 50px;
}
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link rel="stylesheet" type="text/css" href="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script type="text/javascript" src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">


<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<button id="audi" class="btn btn-primary btn-block btnCustom" data-toggle="modal" data-target="#myModal">Open modal with Audi Selected</button>

<button id="mercedes" class="btn btn-primary btn-block btnCustom" data-toggle="modal" data-target="#myModal">Open modal with Mercedes Selected</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      
      <div class="modal-body">
        <select id="selection">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
        </select>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>