How to hide parent element by clicking on child button in jQuery

情到浓时终转凉″ 提交于 2019-12-08 05:30:32

问题


Hi I have following HTML code and I want to hide div with class editfield and editfieldlink link and show div with class value. I tried following jQuery code it did not worked for me. But this works fine if I hide a parent like this statement $(this).closest('div.editfield').parent().hide();

<tr class="row-fluid settingsrow">
                <td class="span2">
                    <strong>Name</strong>
                </td>

                <td class="span8">
                <div class="value">ALI ASAD</div>
                <div class="editfield">
                    <form class="form-vertical">         
                        <div class="control-group">
                            <label class="control-label">First Name</label>
                            <div class="controls">
                                <input type="text" name="firstname" id="firstname" placeholder="Ali">
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">Last Name</label>
                            <div class="controls">
                                <input type="text" name="lastname" placeholder="Asad">
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="controls">
                                <button type="submit" class="btn btn-primary btn-mini">Save Changes</button>
                                <button class="btn btn-inverse btn-mini cancelactionsettings">Cancel</button>
                            </div>
                        </div>
                    </form> 
                </div>
                </td>
                <td class="span1"><a href="#" class="editfieldlink" style="text-decoration:none"><small>Edit</small></a></td>
            </tr>

And My Java Script is like

$(document).ready(function(e) 
{
$('#settingseditor .cancelactionsettings').click(function(event)
{
    event.preventDefault();

    $(this).closest('div.editfield').hide();
    $(this).closest('div.value').show();
    $(this).closest('a.editfieldlink').hide();

});

});


回答1:


You just aren't traversing correctly

$(document).ready(function (e) {
    $('#settingseditor .cancelactionsettings').click(function (event) {
        event.preventDefault();
        $(this).closest('div.editfield').hide();
        $(this).closest('div.editfield').prev('.value').show(); // .value is a prev sibling of div.editfield
        $(this).closest('td').next().find('a.editfieldlink').hide();// anchor is in the next sibling td
    });
});

fiddle




回答2:


try parents().

$('#settingseditor .cancelactionsettings').click(function(event)
{
  event.preventDefault();

  $(this).parents('div.editfield').hide();
  $(this).parents('div.editfield').prev().show();
  $(this).parents('td.span8').next().find('a.editfieldlink');

});

note: i didn't find #settingseditor in your code....if incase , you are trying to use multiselecter then, you forgot , operator there...

$('#settingseditor , .cancelactionsettings').click(...
 //---------------^^^^--- here



回答3:


Not sure where the element with id #settingseditor lives, but it works for me if I am looking for just the .cancelactionsettings element

$('#settingseditor, .cancelactionsettings').click(function(event)
{
   ...
}

jsFiddle




回答4:


Your selector doesn't match any element ; change this $('#settingseditor .cancelactionsettings') to $('.cancelactionsettings').

See this fiddle for a working example



来源:https://stackoverflow.com/questions/15525532/how-to-hide-parent-element-by-clicking-on-child-button-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!