2016-11-19 55 views
0

我有10個<input type='checkbox'>複選框和旁邊我有10個<input type='file'>文件jQuery的:檢查下一個文件輸入不爲空(選擇)

我有這個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一個上點擊下一個按鈕

謝謝

回答

1

編輯II:

然後先找到在這樣做了,然後找到其TD在其中輸入的文件放在你的父母TR。

$("input#imagecheck").each(function(index, element) { 
    if($(element).is(":checked") == true) { 
     if($(this).closest('tr').find("td > input[type=file]").val() == '') { 
     $('#dialogSetter').text("Please Select the File of position ="+(index +1)); 
      $("#dialog").dialog("open"); 
      this.focus(); 
      valid= false; 

      return false; 
     } 
    } 
    });//End of Checkbox Check Function  

的jsfiddle打轉轉:https://jsfiddle.net/8wpLyxbj/

+0

好吧,讓我試試,但究竟是什麼原因上點擊 –

+0

@shantaram我假設你的支票將根據用戶的選擇可以由用戶撤消是動態的,漸進? – ScanQR

+0

不,我只是給你一個示例HTML,我已經與生成的HTML –

1

獲取祖先每個checkboxtr和查找文件輸入它。然後你可以像下面一樣輕鬆地檢查文件輸入。

$("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 
    } 
}); 
+0

感謝和熱情的問候,你的'最近的()'和'find()' –