jQuery:检查下一个文件输入是否为空(选中)

时间:2016-11-19 07:01:36

标签: jquery html

我有10个<input type='checkbox'>复选框,我们旁边有10个<input type='file'>个文件

我有 HTML

<tr>
<td align="center">
<input  name="imagecheck" type="checkbox" value="0" required="required">
</td>
<td align="left">       
    <label  class="Label_Green">Photograph</label>
</td>
<td>        
    <input id=Photograph name=myFile  type="file">
</td>           
<td colspan="1"></td>
</tr>

我想查看,取决于复选框已选中,我还希望用户选择文件

为此,我想查看next()文件输入值,但我无法获取,我应该使用什么

到目前为止,我尝试了这个,我使用了我的高级代码,所以忘了var $this = $(this);

$("input[name=imagecheck]").each(function( index, element ) {
    var $this = $(this);    
    if( $this.is(":checked") ){
        console.log((index +1)+" : Checked");
        alert($this.next("input").prop('name') +" -:- "+$(this).next("input").prop("type"));
        alert($this.next("input").val() +" -:- "+$(this).next("input").val());
        alert($this.nextAll("input").first().prop('name') +" -:- "+$(this).nextAll("input").first().prop("name"));
        if($this.nextAll("input[type=file]").first().val() == '') {
            //tempcounter = index +1;
            $('#dialogSetter').text("Please Select the File of position ="+(index +1));
            $( "#dialog" ).dialog( "open" );
            //tempcounter = 0;
            this.focus();
            valid= false;
            return false;
        }
    }
});//End of Checkbox Check Function                     

已更新

点击下一步按钮

,这是TAB之一的验证部分

谢谢

2 个答案:

答案 0 :(得分:1)

编辑II:

然后首先找到完成此操作的父tr,然后找到放置文件输入的td。

while ((read = getline(&line, &len, fp)) != -1) {
    ...
    free(line);
}

jsfiddle来玩:https://jsfiddle.net/8wpLyxbj/

答案 1 :(得分:1)

获取每个tr的祖先checkbox并找到其中的文件输入。然后您可以轻松检查文件输入,如下所示。

$("input[name=imagecheck]").each(function(index, element) {
    var $this = $(this),
        file = $this.closest('tr').find('input[type=file]')[0];

    if ($this.is(":checked") && !file.files.length) {
        alert("Please Select the File of position =" + (index + 1));
        // show your dialog here instead of alert
    }
});
相关问题