如何在按钮提交前使用jquery禁用div

时间:2014-06-18 06:58:51

标签: jquery

我有两个div,叫做#34;是"和"不"

<div id="object">
    <div class="object-content">
        I'd say
        <div class="confirm" id="btnYes" runat ="server" >Yes</div>
    </div>
    <div class="object-content" style="float: right">
        I'd say
        <div class="confirm" style="color: red"  id="btnNo" runat="server">No</div>
    </div>
</div>

一个评论按钮名为&#34; Post&#34;

<asp:Button ID="btnPostUrComment" OnClick="btnPostUrComment_Click" runat="server" Text="Post Your Comment" Height="33px" class="ppig" />

当我直接点击&#34; Post&#34;我提醒&#34;请点击它们选择是或否&#34; 然后点击&#34;是&#34;或&#34;不&#34; div我警惕&#34;你说&#34;是&#34;或&#34;不&#34; &#34;

现在的问题是,毕竟这个如果我点击&#34;是&#34; div然后&#34; no&#34;在点击&#34;发布&#34;之前,div将被禁用而不会隐藏按钮,反之亦然。

我正在尝试一些脚本。请指导我。

<script>
$(document).ready(function () {
    var IsPostComment = false;
    var clickedElement = "";

    $('.confirm').click(function () {
        IsPostComment = true;
        clickedElement = $(this).text();
    });

    $('#<%=btnPostUrComment.ClientID %>').click(function () {
        if (IsPostComment) {
            alert("You Said " + clickedElement);

        } else {
            alert("Please select Yes or NO by clicking on them")
            return false;
        }
    });
});
</script>

3 个答案:

答案 0 :(得分:0)

这是正确的方法

$('.confirm').click(function () {
        IsPostComment = true;
        clickedElement = $(this).text();
        var x=$(this).attr("id");
        if(var x=="btnYes")
        {
               $('#btnNo').disable();
        }
      else
      {
              $('#btnYes').disable();
       }
    });

答案 1 :(得分:0)

试试这个:

<script>
$(document).ready(function () {
    var IsPostComment = false;
    var clickedElement = "";

    $('.confirm').click(function () {
        // disable other button
        var disableBtnId = ($(this).attr('id')=='btnYes')?'btnNo':'btnYes';
        $('#'+disableBtnId).prop('disabled',true);

        IsPostComment = true;
        clickedElement = $(this).text();
    });

    $('#<%=btnPostUrComment.ClientID %>').click(function () {
        // enable both buttons
        $('.confirm').removeProp('disabled');

        if (IsPostComment) {
            alert("You Said " + clickedElement);

        } else {
            alert("Please select Yes or NO by clicking on them")
            return false;
        }
    });
});
</script>

答案 2 :(得分:0)

试试这个

<script>
$(document).ready(function () {
    var IsPostComment = false;
    var clickedElement = "";

     $('.confirm').click(function () {
            IsPostComment = true;
            clickedElement = $(this).text();
            var mydiv=$(this).attr("id");
            if(mydiv=="btnYes")
            {
                   $('#btnNo').hide();
                   $('#btnYes').show();
            }
          else
          {
                  $('#btnYes').hide();
                  $('#btnNo').show();
           }
        });

    $('#<%=btnPostUrComment.ClientID %>').click(function () {
        if (IsPostComment) {
            alert("You Said " + clickedElement);

        } else {
            alert("Please select Yes or NO by clicking on them")
            return false;
        }
    });
});
</script>