开发者

Disable input submit button with js in IE & FF doesnt' work

开发者 https://www.devze.com 2023-04-09 00:46 出处:网络
I have a problem, with disabling a input button with javascript in a aspx开发者_运维知识库 document at ie.

I have a problem, with disabling a input button with javascript in a aspx开发者_运维知识库 document at ie.

The js look's like

<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) 
{
    if (searchinput.value.length < 4) 
    {
        document.getElementById(target).disabled = true;
    }
    else 
    {
        document.getElementById(target).disabled = false;
    }
}
</script>

I call the input button with

<input name="searchinput" type="text" value="" id="searchinput" onkeyup="SetButtonStatus(this, 'searchsubmit')" />

In Chrome everything works fine. If i type more then 4 characters in the inputfield, the button will be enabled. But in IE & FF nothing happens... Why? How could i fix this?


You are depending on the non-standard "Create a global variable for every element that has an id" that is supported by Chrome and IE in some rendering modes.

Replace searchinput with sender (since you have defined sender and passed a reference to the element you are interested in already).


<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) 
{
    // use document.getElementById("searchinput") instead of searchinput or in your case can use sender 
    document.getElementById(target).disabled = document.getElementById("searchinput").value.length < 4;
}
</script>


Why don't you just use jquery it handles all browsers internaly and you don't have to worry about them. Make it like this:

<input name="searchinput" type="text" value="" id="searchinput"/>
<input type="button" value="button" disabled="true" id="buttonSearch"/>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>  

<script type="text/javascript">

    $(function(){
      $('#searchinput').keydown(function(e){

        var lenght = 3;

        if(e.keyCode ==8)
        {
            lenght = 5;
        }

         if (searchinput.value.length < lenght) 
          {
            $('#buttonSearch').attr("disabled", true);
          }
          else
          {
           $('#buttonSearch').removeAttr('disabled');
          }

       });

    });
</script>
0

精彩评论

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

关注公众号