简体   繁体   English

在选择标记上显示/隐藏div

[英]Show/hide div on select tag

I have tried several time to hide div in laravel framework but the code is not working. 我已经尝试了几次在laravel框架中隐藏div但代码不起作用。 There is some error with my JS code, I can't find it. 我的JS代码有一些错误,我找不到它。 Although its working in js fiddle: http://jsfiddle.net/FvMYz/3823/ 虽然它在js小提琴工作: http//jsfiddle.net/FvMYz/3823/

There are 2 fields in a form: User Type & Role. 表单中有两个字段:用户类型和角色。 When a user selects User Type as "Owner" I want the Role field to get hide else Role field should be displayed. 当用户选择“用户类型”为“所有者”时,我希望“角色”字段隐藏,否则应显示“角色”字段。

<select id="basic" name="usertype" class="selectpicker show-tick form-control" >

   @if(isset($user->usertype))  
        <option value="Owner" type="dropdown" @if($user->usertype=='Owner') selected  @endif >Owner</option>
        <option value="User" type="dropdown" @if($user->usertype=='User') selected @endif >User</option>
    @else
        <option value="Owner" type="dropdown">Owner</option>
        <option value="User" type="dropdown">User</option>                                 
    @endif 



</select>
<script type="text/javascript">

    $(document).ready(function(){
        $('#basic').click(function(){
            if($(this).attr('value')=="Owner"){
            $('div#user_role').hide();
        }
        else{
            $('div#user_role').show();
        }
        });
    });
</script>
<div class="form-group" id="user_role">
    <label for="" class="col-sm-3 control-label">Role</label>
    <div class="col-sm-9">
        <select name="role" id="" class="form-control">
             <option value="student" >Student</option>
             <option value="faculty">Faculty</option>
             <option value="management">Management</option>
         </select> 
    </div>
</div>

Your code is correct, you just need to add $('#user_role').hide(); 你的代码是正确的,你只需要添加$('#user_role').hide(); on onload as your script is getting called on onclick event so it gets called after click and your default option is owner. on onload因为你的脚本在onclick事件上被调用,因此在点击后调用它,你的默认选项是所有者。

See fiddle: http://jsfiddle.net/FvMYz/3825/ 见小提琴: http//jsfiddle.net/FvMYz/3825/

The problem is that you are hiding the select on click, try with change event. 问题是您在点击时隐藏了选择,尝试使用更改事件。

 $(document).ready(function(){ $(function(){ $("#basic").change(function(){ var value = $(this).val(); var user_role = $("#user_role"); if(value == "Owner"){ user_role.hide(); }else{ user_role.show(); } }); }); }); 
 #user_role{ display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <select id="basic" name="usertype"> <option value="Owner" >Owner</option> <option value="User">User</option> </select> <select id="user_role"> <option value="student">Student</option> <option value="faculty">Faculty</option> <option value="management">Management</option> </select> 

显示/隐藏<div>标签不工作</div><div id="text_translate"><p>我有一个超链接,它应该在 onclick 事件期间一一调用 3 个 JS 函数。</p><pre> &lt;form name = "bulkcontactfrm" method="POST" action="&lt;%= servletPath %&gt;&gt; &lt;div id="saveDiv" layoutAlign="top" style="display:block;"&gt; &lt;table id="" align="left" border="0" cellpadding="0" cellspacing="0"&gt; &lt;tr&gt; &lt;td&gt; &lt;a href="javascript:void(0);" onclick="isAllowedToResubscribe(document.bulkcontactfrm); manipulateDIV(document.bulkcontactfrm); resubscribeCall(document.bulkcontactfrm);"&gt;&amp;#160;Re-Subscribe&lt;/zoniac:roundrect&gt;&amp;#160;&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div id="loadingDiv" class="cellWhiteBGFont" layoutAlign="top" style="display: block;"&gt;&lt;p&gt;&lt;img src="&lt;%=ImageMappingManager.getImageName("imgLoading")%&gt;" name = "b1"&gt;&amp;nbsp;&amp;nbsp;&lt;font size='3'&gt;&lt;b&gt;Please wait...&lt;b&gt;&lt;/font&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt;</pre><p> JS函数如下:</p><pre> // First function validate the data using ajax call function isAllowedToResubscribe(form) { //Client validation takes here processAjaxRequestPost('ajaxRequestPost','SimpleHandler','getResubscribeEmailValidationDetails',emilIDStr,sourcefromStr); } // Second function hide the content in UI and show the Processing image in &lt;DIV&gt; tag function manipulateDIV(form) { hideSaveDiv(); showLoadingDiv(); } function hideSaveDiv() { //hide the Re-Subscribe hyperlink document.getElementById('saveDiv').style.display='none'; } function showLoadingDiv() { //show the Processing image document.getElementById('loadingDiv').style.display='block'; } // Third function is for form submit using ajax call function resubscribeCall(form) { //processAjaxRequestPost('ajaxRequestPost','SimpleHandler','getResubscribeEmailDetails',emilIDStr,sourcefromStr); }</pre><p> 单击超链接验证 function 调用并获取成功后,出现构造消息,单击构造上的确定。 但是&lt;DIV&gt;标签并没有被隐藏,所以没有加载进度图像。</p></div> - show/hide the <div> tag is not working

暂无
暂无

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

相关问题 jQuery-使用select标签显示和隐藏DIV - jquery - show and hide DIV with the select tag 在选择时显示/隐藏div - Show/hide div on select 显示/隐藏div标签javascript - Show/Hide div tag javascript 显示/隐藏<div>标签不工作</div><div id="text_translate"><p>我有一个超链接,它应该在 onclick 事件期间一一调用 3 个 JS 函数。</p><pre> &lt;form name = "bulkcontactfrm" method="POST" action="&lt;%= servletPath %&gt;&gt; &lt;div id="saveDiv" layoutAlign="top" style="display:block;"&gt; &lt;table id="" align="left" border="0" cellpadding="0" cellspacing="0"&gt; &lt;tr&gt; &lt;td&gt; &lt;a href="javascript:void(0);" onclick="isAllowedToResubscribe(document.bulkcontactfrm); manipulateDIV(document.bulkcontactfrm); resubscribeCall(document.bulkcontactfrm);"&gt;&amp;#160;Re-Subscribe&lt;/zoniac:roundrect&gt;&amp;#160;&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div id="loadingDiv" class="cellWhiteBGFont" layoutAlign="top" style="display: block;"&gt;&lt;p&gt;&lt;img src="&lt;%=ImageMappingManager.getImageName("imgLoading")%&gt;" name = "b1"&gt;&amp;nbsp;&amp;nbsp;&lt;font size='3'&gt;&lt;b&gt;Please wait...&lt;b&gt;&lt;/font&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt;</pre><p> JS函数如下:</p><pre> // First function validate the data using ajax call function isAllowedToResubscribe(form) { //Client validation takes here processAjaxRequestPost('ajaxRequestPost','SimpleHandler','getResubscribeEmailValidationDetails',emilIDStr,sourcefromStr); } // Second function hide the content in UI and show the Processing image in &lt;DIV&gt; tag function manipulateDIV(form) { hideSaveDiv(); showLoadingDiv(); } function hideSaveDiv() { //hide the Re-Subscribe hyperlink document.getElementById('saveDiv').style.display='none'; } function showLoadingDiv() { //show the Processing image document.getElementById('loadingDiv').style.display='block'; } // Third function is for form submit using ajax call function resubscribeCall(form) { //processAjaxRequestPost('ajaxRequestPost','SimpleHandler','getResubscribeEmailDetails',emilIDStr,sourcefromStr); }</pre><p> 单击超链接验证 function 调用并获取成功后,出现构造消息,单击构造上的确定。 但是&lt;DIV&gt;标签并没有被隐藏,所以没有加载进度图像。</p></div> - show/hide the <div> tag is not working 在 HTML 中隐藏和显示 Div 标签 - Hide and Show Div tag in HTML 隐藏身体标签,但仍显示div - hide body tag but still show a div 显示特定的div并基于iframe AJAX / jQuery中的选择标记隐藏其他div? - Show specific div and hide others based on select tag in iframe AJAX/jQuery? 在选择标签中选择选项后隐藏div - Hide div after selecting option in select tag HTML Select标签和jQuery显示和隐藏 - HTML Select Tag and jQuery Show and Hide Div 不显示/隐藏单选按钮选择 - Div not show/hide on radio button select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM