简体   繁体   English

Rails 5:在使用JS进行编辑操作时显示选定的值

[英]Rails 5: show selected value at edit action with JS

In View I have drop-down, where User can be selected: 在“视图”中,有一个下拉菜单,可以在其中选择“用户”:

   <!-- more code above -->
<% for role in user.roles %>
<div class="col-sm-10"><select class="form-control m-b" name="users" id="user_list">
   <option value="<%= user.id %>" data-edit-url="<%= edit_common_role_path(role.id) %>"><%= user.name %></option>

This JS script on select, opens role of selected User by role ID for edit (eg, /common/roles/1/edit ) 选中该JS脚本后,将按角色ID打开所选用户的角色以进行编辑(例如/ common / roles / 1 / edit

<script type="text/javascript">
            edit = $('#user_list').change(function() {
            window.location = $(this).find(":selected").data('edit-url');
            });
</script>

How to achieve that when Edit path is opened, in drop-down menu selected User name is shown? 打开“编辑路径”后,如何在下拉菜单中选择“用户名”? At the moment it always get 1st value in list. 此刻,它始终在列表中获得第一个值。 I'm on Rails 5, Bootstrap. 我在Rails 5,Bootstrap上。 Thank you for any help! 感谢您的任何帮助!

I am not aware of rails. 我不知道铁轨。 But, can you cross check once if you it is edit-url not data-edit-url . 但是,如果您是edit-url而不是data-edit-url ,是否可以交叉检查一次。

And also instead of $(this).find(":selected").data('edit-url'); 并且代替$(this).find(":selected").data('edit-url'); simply use $(this).data('edit-url') . 只需使用$(this).data('edit-url')

I have a small working example in Plain HTML and Jquery. 我在Plain HTML和Jquery中有一个小的工作示例。

<html>

<head>
<script   src="https://code.jquery.com/jquery-3.1.1.js"   
        integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous"></script>    
</head>

<body>

    <select id="url">
        <option value="Please Select">1</option>
        <option value="https://google.com">2</option>        
    </select>

</body>

<script>

    $('#url').change(function(){
                     window.location = $(this).val();
                     });

</script>    


</html>

I hope, it will help. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM