如何从不同输入中的模态中获取价值

时间:2019-04-03 08:34:11

标签: javascript php jquery html

我必须写一个界面管理文章,上面有几个按钮,单击这些按钮将引发对话框(模式)。到目前为止,一切都很好。

在模式中,我在下拉列表中有一个值。当我单击“保存”时,我设法在第一个“输入”中显示选定的元素,但是对于其他按钮,它不起作用。

$(document).ready(function(){
            $("[id^=btn_]").on('click', function(){
                var boutton_selectionne = this.id
                console.log(boutton_selectionne)
                $("#monModal").modal('show')
            })
            $('#sauvegarder').on('click', function(){
                var btn1 = $('#nbre_compart option:selected').text()
                $('#txt_btn1').val(btn1)
                return
            })   
        });
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
    <button class="btn btn-info" id="btn_mdl1">Boutton 1</button>
    <input type="text" name="btn_1" id="txt_btn1">
    <button class="btn btn-info" id="btn_mdl2">Boutton 2</button>
    <input type="text" name="btn_2" id="txt_btn2">
    <button class="btn btn-info" id="btn_mdl3">Boutton 3</button>
    <input type="text" name="btn_3" id="txt_btn3">
    <button class="btn btn-info" id="btn_mdl4">Boutton 4</button>
    <input type="text" name="btn_4" id="txt_btn4">
<div id="monModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Confirmation</h4>
            </div>
            <div class="modal-body">
                    <label for="nbre_compart">Nombre de compartiments</label>
                    <select id="nbre_compart">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6" selected>6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                        </select> 
            </div>
            <div class="modal-footer">
                <button type="button" id="sauvegarder" class="btn btn-primary">Sauvegarder</button>
            </div>
        </div>
    </div>
</div>
</body>

1 个答案:

答案 0 :(得分:1)

嗨,现在其他按钮都可以使用了:), 库没有出现在您的代码中,这就是为什么它对模型函数造成错误的原因。

$(document).ready(function(){
            $("[id^=btn_]").on('click', function(){
                var boutton_selectionne = this.id
                console.log(boutton_selectionne);
               $('#sauvegarder').attr("data-id",this.id);
                $("#monModal").modal('show');
            })
            $('#sauvegarder').on('click', function(){
           

console.log("daf"+$('#sauvegarder').attr("data-id")); 

 var btn1 = $('#nbre_compart option:selected').text();
                $('#'+'txt_'+$('#sauvegarder').attr("data-id")).val(btn1)
                return
            })   
        });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
<body>
    <button class="btn btn-info" id="btn_mdl1">Boutton 1</button>
    <input type="text" name="btn_1" id="txt_btn_mdl1">
    <button class="btn btn-info" id="btn_mdl2">Boutton 2</button>
    <input type="text" name="btn_2" id="txt_btn_mdl2">
    <button class="btn btn-info" id="btn_mdl3">Boutton 3</button>
    <input type="text" name="btn_3" id="txt_btn_mdl3">
    <button class="btn btn-info" id="btn_mdl4">Boutton 4</button>
    <input type="text" name="btn_4" id="txt_btn_mdl4">
<div id="monModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Confirmation</h4>
            </div>
            <div class="modal-body">
                    <label for="nbre_compart">Nombre de compartiments</label>
                    <select id="nbre_compart">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6" selected>6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                        </select> 
            </div>
            <div class="modal-footer">
                <button type="button" id="sauvegarder" class="btn btn-primary">Sauvegarder</button>
            </div>
        </div>
    </div>
</div>
</body>