从所选选项中获取 href 值并更改按钮的 href

get the href value from selected option and change the href of the button

我想获取所选选项的 href 值,以便我现在可以转到我使用按钮选择的 link。

这是我的代码

<div class="modal fade" id="soa">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Choose the name of student</h4>
                </div>

                <div class="modal-body">
                    <div class="col-md-12">
                         <div class="form-group">
                            <label class="control-label"><span
                               class="text-danger"></span> Name of Student</label> 
                            <select name="childNames" class="form-control input-lg" id="childNames" data-size="5">
                               <option value="" selected disabled></option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice">John Dela Cruz</option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice1">Josh Dela Cruz </option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice2">Jane Dela Cruz</option>
                            </select>
                         </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href=''><button type="button" class="btn btn-info">View</button></a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

添加一个id =>

<a id="link" href=''><button type="button" class="btn btn-info">View</button></a>

这应该有效:

$(document).ready(function() {

    $("#childNames").on('change', function () {

                var ref = document.getElementById("childNames").value;
                alert(ref);
                $('#link').attr('href', ref);

    });

});

jsfiddle example

这里还有备选答案。只需将它添加到您的脚本中

     <script type="text/javascript" >
        jQuery(document).ready(function($) {

            $("#childNames").change(function () {
                        var ref =$(this).find('option:selected').attr('value');
                        $('#link').attr('href', ref);

            });

        });
     </script>