dongzhuang6177
dongzhuang6177
采纳率100%
2016-07-13 10:35

如何在jquery中只显示一个特定的div

I am trying to display one specified div when I am clicking on button with class .showFormButton, but unfortunetly every div with class .showForm appears. How to make only one .showForm div appear using jQuery? JQUERY:

$(document).ready(function() {
$(".showFormButton").click(function(e){
e.preventDefault();
$(".showForm").css('display', 'block');
$(".showForm").css('position', 'absolute');
});
});

HTML

print "<input type='submit' class='showFormButton' VALUE='Ulubiony' onclick='ShowDiv()'>";

    print "<div style='display: none; margin-left: -170px; margin-top: -60px; background-image: linear-gradient(rgb(15, 159, 220),rgb(14, 108, 148));' class='showForm'>";
        print "<div >";
            print "<p><b>Dodaj do ulubionych pracownika</b></br> [".$row['imie1']. " ".$row['nazwisko1']."]</p>";
            print "<p>Osobista notatka</p>";
            print "<FORM ACTION='view_tm.php' METHOD='POST' ENCRYPT='multipart/form-data' name='ulubiony_pracownik_dodaj'>";
            print "<textarea name='id_pracownik_op_notatka' rows='10' cols='45'>". $row['notatka'] ."</textarea>";
            print "<input type='hidden' name='id_pracownik_op' value='" . $row['id_pracownik'] . "'>";
            print "<INPUT TYPE='submit' id='showFormButton' NAME='ulubiony_pracownik' VALUE='Dodaj do ulubionych'>";
            print "</form>";
        print "</div>";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dqsxsmi3704 dqsxsmi3704 5年前

    You have to use this object to target your selection over the clicked element,

    $(document).ready(function() {
       $(".showFormButton").click(function(e){
         e.preventDefault();
         var elm = $(".showForm", this)
         elm.css({'display' : 'block', 'position' : 'absolute' });
       });
    });
    

    Instead of using .css() for setting the properties, you could use .addClass(). Because it would be easy in maintaining, like removing the added properties in future.

    $(document).ready(function() {
       $(".showFormButton").click(function(e){
         e.preventDefault();
         var elm = $(".showForm", this)
         elm.addClass("clicked");
       });
    });
    

    CSS:

    .clicked { display:block; position:absolute; }
    

    Edit:

    Since the target element is the next sibling the the current element, you have to use .next(selector) at this context.

    $(document).ready(function() {
       $(".showFormButton").click(function(e){
         e.preventDefault();
         var elm = $(this).next(".showForm")
         elm.css({'display' : 'block', 'position' : 'absolute' });
       });
    });
    
    点赞 评论 复制链接分享
  • dongpigui8898 dongpigui8898 5年前

    You can distinguish the div by using an attribute

    $(document).ready(function() {
    $(".showFormButton").click(function(e){
    e.preventDefault();
    $(".showForm[showme='1']").css('display', 'block');
    $(".showForm[showme='1']").css('position', 'absolute');
    });
    });
    

    HTML

    print "<input type='submit' class='showFormButton' VALUE='Ulubiony' onclick='ShowDiv()'>";
    
        print "<div showme='1' style='display: none; margin-left: -170px; margin-top: -60px; background-image: linear-gradient(rgb(15, 159, 220),rgb(14, 108, 148));' class='showForm'>";
            print "<div >";
                print "<p><b>Dodaj do ulubionych pracownika</b></br> [".$row['imie1']. " ".$row['nazwisko1']."]</p>";
                print "<p>Osobista notatka</p>";
                print "<FORM ACTION='view_tm.php' METHOD='POST' ENCRYPT='multipart/form-data' name='ulubiony_pracownik_dodaj'>";
                print "<textarea name='id_pracownik_op_notatka' rows='10' cols='45'>". $row['notatka'] ."</textarea>";
                print "<input type='hidden' name='id_pracownik_op' value='" . $row['id_pracownik'] . "'>";
                print "<INPUT TYPE='submit' id='showFormButton' NAME='ulubiony_pracownik' VALUE='Dodaj do ulubionych'>";
                print "</form>";
            print "</div>";
    
    点赞 评论 复制链接分享
  • dongzenglin8292 dongzenglin8292 5年前

    jQuery has the .show() function to make hidden stuff visible. Take a look at this code snippet:

    $(document).ready(function () {
        $(document).on('click', '.action-btn', function () {
            $('.hidden-div').show();
        });
    ));
    
    <div style="display: none;" class="hidden-div">
        Hidden text
    </div>
    
    <button class="action-btn">Show hidden div</button>
    
    点赞 评论 复制链接分享

为你推荐