为了账号安全,请及时绑定邮箱和手机立即绑定

根据页面加载时的下拉值显示文本框

根据页面加载时的下拉值显示文本框

慕莱坞森 2021-09-30 16:40:28
我需要根据页面加载下拉菜单中选择的值显示一个文本框。我的下拉菜单的 id - 文本框的 #inputtype id 是 #checkboxvalues。我只是在做简单的“如果输入类型是复选框显示复选框值。它似乎不起作用。但 on(change) 功能工作正常。也就是说,如果我将下拉列表更改为其他内容并选择“复选框,它会显示正确的文本框。谢谢你的时间。$(document).ready(function(){    var inputtype = $( "#inputtype" ).val();    if (inputtype === "Checkbox"){        $("#checkboxvalues").show();    }    if (inputtype != "Checkbox"){        $("#checkboxvalues").hide();    }    if (inputtype != "Radio"){        $("#radiovalues").hide();    }    if (inputtype != "Checkbox with Other"){        $("#checkboxvalues").hide();    }    if (inputtype != "Radio with Other"){        $("#radiovalues").hide();    }    $('#inputtype').on('change',function(){        var inputtype = $( "#inputtype" ).val();        if (inputtype === "Checkbox"){            $("#checkboxvalues").show();            $("#radiovalues").hide();        } else if (inputtype === "Radio") {            $("#radiovalues").show();            $("#checkboxvalues").hide();        }  else if (inputtype === "Checkbox with Other") {            $("#checkboxvalues").show();            $("#radiovalues").hide();        }  else if (inputtype === "Radio with Other") {            $("#radiovalues").show();            $("#checkboxvalues").hide();        }         else {            $("#checkboxvalues").hide();            $("#radiovalues").hide();        }    });
查看完整描述

3 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

您在此行的加载功能中隐藏文本框:


if (inputtype != "Checkbox with Other"){

    $("#checkboxvalues").hide();

}

作为代码审查问题,您可能希望将所有逻辑放在一个函数中,以便只调用一次。


$(document).ready(function(){

    function updateInputs() {

        var inputtype = $( "#inputtype" ).val();

        $("#checkboxvalues").toggle(inputtype === "Checkbox" || inputtype === "Checkbox with Other");

        $("#radiovalues").toggle(inputtype === "Radio" || inputtype === "Radio with Other");

    }

    updateInputs();

    $('#inputtype').on('change', updateInputs);    

});


查看完整回答
反对 回复 2021-09-30
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

这应该有效,

var inputtype = $( "#inputtype option:selected" ).val();

希望能帮助到你!

并尝试使用数组而不是比较每个单独的文本,(editedsitefield.fieldinputtype == "Text Box")即在数组中存储“文本框”之类的值,循环遍历它。


查看完整回答
反对 回复 2021-09-30
  • 3 回答
  • 0 关注
  • 295 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号