开发者

applying a var to an if statement jquery

Need to apply a var to a statement if its conditions are met, this syntax isn't throwing errors but its not working.

<script type="text/javascript">
$(document).ready(function(){
  var action_is_post = false;
      //stuff here
    $(this).ready(function () {
        if ($("#stepDesc0").is(".current")) {
        action_is_post = true;     
        }
    });
  //stuff here
</script>

should I use something other than .ready? Do I even need the $(this).ready(function ()... part? I need it to apply the var when #stepDesc0 has the class current.

$(document).ready(function(){ var action_is_post = false; $("form").submit(function () { action_is_post = true; }); if ($("#stepDesc0").is(".current")) { var action_is_post = true; } window.onbeforeunload = confirmExit; function confirmExit() { if (!action_is_post) return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the 开发者_如何转开发form.'; } });


<script type="text/javascript">
  $(document).ready(function(){
    var action_is_post=$("#stepDesc0").is(".current");
  });
</script>

If you want the variable to be accessible outside the $(document).ready(function(){..., then you'll need to declare it outside the statement like this:

<script type="text/javascript">
  var action_is_post;

  $(document).ready(function(){
    action_is_post=$("#stepDesc0").is(".current");
  });
</script>

HTML (in order to test it):

<a href="javascript:alert(action_is_post);">Show value</a>


$(function() {
    var actionIsPost = $('#stepDesc0').is('.current'); 
    alert( actionIsPost );
});

If you are adding the current class to #stepDesc0 on an event then put the .is check in the event handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜