最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML input textbox onChange not working - Stack Overflow

programmeradmin8浏览0评论

This is what I have:

<script language="JavaScript">
    function toggle() {
        if (this.value=='1') {
            document.getElementById('dbOn').style.visibility='visible';
        }
        else {
            document.getElementById('dbOn').style.visibility='hidden';
            document.getElementById('dbOff').checked='checked';
        }
    }
</script>

<form action="index" method="post">
    <input type="text" name="iterations" value="1" onChange="toggle()" >

    <input type="radio" name="db" value="OFF" checked="checked" id="dbOff" >OFF<br>
    <input type="radio" name="db" value="ON" id="dbOn" >ON
</form>

The idea is that you will only be allowed to write to the database if you are doing one iteration. Otherwise, I want the "ON" option to bee disabled or disappear and for OFF to be checked automatically.

So far, I have tried onChange, onKeyUp and onKeyPress, but none of them appear to work. Any idea what is wrong with my code?

Thanks

This is what I have:

<script language="JavaScript">
    function toggle() {
        if (this.value=='1') {
            document.getElementById('dbOn').style.visibility='visible';
        }
        else {
            document.getElementById('dbOn').style.visibility='hidden';
            document.getElementById('dbOff').checked='checked';
        }
    }
</script>

<form action="index" method="post">
    <input type="text" name="iterations" value="1" onChange="toggle()" >

    <input type="radio" name="db" value="OFF" checked="checked" id="dbOff" >OFF<br>
    <input type="radio" name="db" value="ON" id="dbOn" >ON
</form>

The idea is that you will only be allowed to write to the database if you are doing one iteration. Otherwise, I want the "ON" option to bee disabled or disappear and for OFF to be checked automatically.

So far, I have tried onChange, onKeyUp and onKeyPress, but none of them appear to work. Any idea what is wrong with my code?

Thanks

Share Improve this question edited Oct 19, 2019 at 0:24 Arepa Slayer asked Jul 15, 2013 at 18:11 Arepa SlayerArepa Slayer 4431 gold badge7 silver badges21 bronze badges 1
  • When setting the checked property, use a boolean - true/false – Ian Commented Jul 15, 2013 at 18:14
Add a ment  | 

1 Answer 1

Reset to default 11

You need to pass a reference to your element when calling the function.

onchange="toggle(this)"

You'll also need a variable for the value in your function:

function toggle(element) {
        if (element.value=='1') {
  ...

}

DEMO: http://jsfiddle/axxCH/1/

发布评论

评论列表(0)

  1. 暂无评论