简体   繁体   中英

How do I disable form dropdown option using javascript

I'm trying to disable the "name" text field in the form when "Choose" is selected in the drop down after the page loads (it's disabled when the page loads) ie after I've chosen one of the other two options that disable or enable that field, when I return to "Choose" i'd like the same field to disable. I can't see why the javascript I've written would prevent this from happening. Thanks!

        <script type="text/javascript">

    function clickclear(thisfield, defaulttext) {
        if (thisfield.value === defaulttext) {
            thisfield.value = "";
        }
    }

    function clickrecall(thisfield, defaulttext) {
        if (thisfield.value === "") {
            thisfield.value = defaulttext;
        }
    }

    function checkPickup() {

        if (form.os0.value != "Pickup from Toowong, Brisbane" ) {
            form.name.disabled = false; form.name.style.color = '#333';

        } else {

            form.name.disabled = true; form.name.style.color = '#CCC';
            /* Reset form values */
            form.name.value = "His/her name";
        }
    }
</script>

<script type="text/javascript">
    function validate(form) {

        var errmsg = "Oops, you're required to complete the following fields! \n";

        // Various other form validations here

        // Validate "Pickup"
        if (form.os0.value === "") {
            errmsg = errmsg + " - Choose pickup or delivery\n";
        }

        // Validate "phone"
        if (form.phone.value === "" || form.phone.value === "Mobile's best!") {
            errmsg = errmsg + " - Your phone number\n";
        }


        if (form.os0.value != "Pickup from Toowong, Brisbane") {

            // Validate "name"
            if (form.name.value === "" || form.name.value === "His/her name") {
                errmsg = errmsg + " - His/her name\n";
            }
        }

        // Alert if fields are empty and cancel form submit
        if (errmsg === "Oops, you're required to complete the following fields! \n") {
            form.submit();
        } else {
            alert(errmsg);
            return false;
        }
    }
</script>
    </head>
    <body>
        <form name="form" action="https://www.paypal.com/cgi-bin/webscr" method="post" onSubmit="return validate(form)">

            <p class="row">
                <input type="hidden" name="on0" value="Pickup and delivery" />Pickup and delivery<br />
                <select name="os0" onchange="checkPickup()">
                    <option value="" selected >Choose</option>
                    <option value="Pickup from Toowong, Brisbane">Pickup from Toowong, Brisbane $1.00 AUD</option>
                    <option value="Brisbane +$23.60">Brisbane +$23.60 =$1.00 AUD</option>
                </select>
            </p>
            <p class="row">Your daytime phone number<br />
                <input type="text" name="phone" value="Mobile's best!" onclick="clickclear(this, 'Mobile\'s best!')" onblur="clickrecall(this,'Mobile\'s best!')" />
            </p>
            <p class="row">Recipient's name<br />
                <input style="color: #ccc" class="name" type="text" name="name" value="His/her name" onclick="clickclear(this, 'His/her name')" onblur="clickrecall(this,'His/her name')" disabled />
            </p>
            <input name="custom" type="hidden"  />
            <input type="hidden" name="currency_code" value="AUD" />
            <input class="button" type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online." />
            <img alt="" border="0" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1"> -->
        </form>
    </body>
</html>

This may be a simple misunderstanding of what you've written:

if (form.os0.value != "Pickup from Toowong, Brisbane" ) {
    form.name.disabled = false; form.name.style.color = '#333';
} else {
    form.name.disabled = true; form.name.style.color = '#CCC';
    //
}

translates to the following in plain english:

If the value is NOT "Pickup from Toowong, Brisbane" , enable the field, otherwise disable it.

which is equivalent to:

ONLY disable the field when the value is "Pickup from Toowong, Brisbane" .

I believe the logic you're looking for is:

if (form.os0.value == "Brisbane +$23.60" ) {
    form.name.disabled = false; form.name.style.color = '#333';
} else {
    form.name.disabled = true; form.name.style.color = '#CCC';
    //
}

though it might be prettier to code this with a switch statement due to the involvement of specific cases.

did you intend to type double equal to (==) or is the triple equal to (===) a typo in the question? Based on looking at your code, it looks to me like you need a double equal to (==) not a triple. I think triple may mean something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM