下拉列表中未选择值,请将内联 js 代码转换为外部 j 查询

value is not selected in dropdown and please convert Inline js code to external j query

您好,您可以将内联 js 代码转换为外部 js 代码吗?我怎么可能试过但没有 possible.And 值也没有在下拉列表中选择,所以怎么可能?我的密码是 hear

<select class="form-control" id="ncats" name="ncats" onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    <option value="/pr192/index.php?id=10">All</option>
    <option value="/pr192/index.php?id=25">Electric</option>
    <option value="/pr192/index.php?id=15">Solar</option>
    <option value="/pr192/index.php?id=19">Real Estate</option></select>

将内联转换为函数

<script>
    function fred(sel) {
        sel.options[sel.selectedIndex].value && (window.location = sel.options[sel.selectedIndex].value);
    }
</script>

然后调用函数,传递 this 作为参数

<select class="form-control" id="ncats" name="ncats" onchange="fred(this);">
    <option value="/pr192/index.php?id=10">All</option>
    <option value="/pr192/index.php?id=25">Electric</option>
    <option value="/pr192/index.php?id=15">Solar</option>
    <option value="/pr192/index.php?id=19">Real Estate</option></select>

Javascript 部分。

    <script>
        function convert(val) {
            val.options[val.selectedIndex].value && (window.location = val.options[val.selectedIndex].value);
            }
    </script>

HTML 部分

            <option value="/pr192/index.php?id=10">All</option>
            <option value="/pr192/index.php?id=25">Electric</option>
            <option value="/pr192/index.php?id=15">Solar</option>
            <option value="/pr192/index.php?id=19">Real Estate</option>
    </select>

希望以上内容对您有所帮助