简体   繁体   中英

Displaying a hidden text box when a particular option value selected

I am trying to make a hidden text-box visible when a particular option value is selected, It works when there are multiple options available obviously because it responds to onChange. How can I get it to work if that is the only option present, the first select box in my Example.

Js Fiddle - http://jsfiddle.net/8bm9R/

This is my Js function

function showOther(fieldObj, otherFieldID) {
    var fieldValue = fieldObj.options[fieldObj.selectedIndex].value;
    var otherFieldObj = document.getElementById(otherFieldID);
    otherFieldObj.style.visibility = (fieldValue == 'other') ? '' : 'hidden';
    return;
} 

I've updated the JsFiddle:

Basically JsFiddle is misused, the function should be set to be wrapped in the header instead of 'onLoad'.

jsfiddle.net/8bm9R/2/

function showOther(fieldObj, otherFieldID)
{

    var fieldValue = fieldObj.options[fieldObj.selectedIndex].value;
    var otherFieldObj = document.getElementById(otherFieldID);

    otherFieldObj.style.visibility = (fieldValue=='other') ? '' : 'hidden';

    return;
}

Cheers

$("select").change(function() {
if($(this).val() == "expected_value") {
otherFieldObj.style.visibility = "visible"
}
else {
otherFieldObj.style.visibility = "hidden"
}
});

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