I need to hide a button on a certain page if a text box has any value.

So far I can it from a specific page using this:

$(document).ready(function() {
     if (window.location.href.indexOf("mypage.aspx") > -1 {
          $('#myButoon').hide();
     }
});

But for the button to be hidden I need to the user to be the specific page (mypage.aspx) and for the text box to have any value (#myTextBox).

有帮助吗?

解决方案

you must add the value of your textbox in your condition like this :

$(document).ready(function() {
    if (window.location.href.indexOf("mypage.aspx") > -1 && $('#myTextbox').val() == 'Text to match' ){
        $('#myButoon').hide();
    }
});

其他提示

This should do what you want:

$(document).ready(function() {
     if (window.location.href.indexOf("mypage.aspx") > -1) {
          // if a button is pressed in the textbox.
          $('#myTextBox').keyDown(function(){
              // if the textbox is now longer empty
              if($(this).val() != ""){
                 $('#myButoon').hide();
              }
          });
     }
});

But checking if you are on a certain url for some actions doesn't seem like the best approach imho. You might want to look for different approach.

try this exampole

$(document).ready(function() {    
     checkTxt();                  
     $("#txt").keyup(function() {checkTxt();});
});


function checkTxt(){
    if (window.location.href.indexOf("fiddle") > -1 && $("#txt").val() == "")
        $("#btn").hide();                  
}

demo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top