选择特定选项值时显示隐藏文本框

时间:2013-06-25 07:36:43

标签: javascript jquery html

我试图在选择特定选项值时使隐藏文本框可见,当有多个选项可用时,它显然是因为它响应onChange。如果这是唯一存在的选项,我的示例中的第一个选择框,我怎样才能使它工作。

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

这是我的Js功能

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

2 个答案:

答案 0 :(得分:2)

我已经更新了JsFiddle:

基本上JsFiddle被滥用,应该将函数设置为包含在标题中而不是'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;
}

干杯

答案 1 :(得分:0)

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