显示基于下拉选择的多文件上传

时间:2018-04-01 20:12:30

标签: c# model-view-controller

我正在尝试运行此代码,但当我选择列表中的任何项目时,我不知道为什么jquery会出现错误 这是代码

<form name="myform">
    <table>
        <td> // green underline in this line of the <td>
            <select name="one" onchange=" 
if(this.options[this.selectedIndex].value == '1')
{
    document.myform['b'].style.visibility = 'visible'
}
else if (this.options[this.selectedIndex].value == '2')
{
    document.myform['b'].style.visibility = 'visible',
    document.myform['m'].style.visibility = 'visible'
}
else if (this.options[this.selectedIndex].value == '3') 
{
    document.myform['b'].style.visibility = 'visible',
    document.myform['m'].style.visibility = 'visible',
    document.myform['p'].style.visibility = 'visible'
}
else
{
    document.myform['b'].style.visibility = 'hidden',
    document.myform['m'].style.visibility = 'hidden',
    document.myform['p'].style.visibility = 'hidden'
} ;">
                <option value="" selected="selected">Select...</option>
                <option value="1">Bachelore Degree</option>
                <option value="2">Master Degree</option>
                <option value="3">PhD</option>
            </select>
            <input type="file" id ="b" style="visibility:hidden;" />
            <input type="file" id ="m" style="visibility:hidden;" />
            <input type="file" id ="p" style="visibility:hidden;" />
        </td>
    </table>
</form>​
@section Scripts {

@Scripts.Render("~/Content/bootstrap-multiselect.css")
@Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
@Scripts.Render("~/Scripts/bootstrap.js")
@Scripts.Render("~/Scripts/bootstrap-multiselect.jsl")
}

如果我从下拉列表中选择1或2或3,则会收到此错误消息

  

0x800a138f - JavaScript运行时错误:无法获取属性&#39; b&#39;未定义或空引用

代码背后的想法是,如果用户选择他的资格作为学士学位持有人只有一个文件,他必须上传,当他选择主人,然后2个文件上传和博士3文件证书

1 个答案:

答案 0 :(得分:0)

我已经尝试过你的html / js代码,并没有看到你提到的错误。

然而,我通过将'onchange'js代码放在一个单独的方法中稍微重新构建了代码,因为它增加了清晰度。希望它有所帮助。

<html>
<script type="text/javascript">
function qualificationSelected(selectedIndex){
    document.getElementById('b').style.visibility = (selectedIndex >= 1) ? 'visible' : 'hidden';
    document.getElementById('m').style.visibility = (selectedIndex >= 2) ? 'visible' : 'hidden';
    document.getElementById('p').style.visibility = (selectedIndex == 3) ? 'visible' : 'hidden';
}
</script>
<body>
<form name="myform">
    <table>
        <tr>
        <td> 
            <select name="one" onchange="qualificationSelected(this.selectedIndex)">
                <option value="" selected="selected">Select...</option>
                <option value="1">Bachelore Degree</option>
                <option value="2">Master Degree</option>
                <option value="3">PhD</option>
            </select>
            <input type="file" id ="b" style="visibility:hidden;" />
            <input type="file" id ="m" style="visibility:hidden;" />
            <input type="file" id ="p" style="visibility:hidden;" />
        </td>
    </tr>
    </table>
</form>​

</body>
</html>