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

时间:2015-07-11 06:19:26

标签: javascript php jquery html html-select

你好,你可以将内联js代码转换为外部js代码,所以我怎么可能尝试但不可能。在dropdownlist中也没有选择值,那怎么可能呢?我的代码是

<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>

2 个答案:

答案 0 :(得分:1)

将内联转换为函数

<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>

答案 1 :(得分:0)

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>

我希望这会对你有所帮助