仅在display:block时验证文本字段

时间:2013-08-30 19:18:39

标签: javascript

我有2个需要验证的文本字段。

  1. 需要验证合并/原因字段
  2. 条形码只有在显示时才需要验证,即如果选中了复选框。
  3. 我要做的是弹出一个警告框,用于合并原因(无论如何),如果没有隐藏,则在警报中为条形码添加验证消息

    以下是代码:

        <tr><td>
           <input type="checkbox" name="createCharge" id="createCharge" onClick="checkBox('Barcode', 'CreateCharge');" value="1"/> 
           <lable>Charge</label>
         </td></tr>
    
    
        <tr id="Barcode" style="display:none;">
        <td>
            <label>Barcode</label>
            <input type="text" name="Barcode" id="Barcode"/>
        </td>
        </tr>
    
        <tr>
        <td>
            <label>Merge:</label>
            <input type="text" name="Reason" id="Reason"/>
        </td>
        </tr>
    

2 个答案:

答案 0 :(得分:2)

你可以这样检查: -

if($(x).is(":visible"))
{
  //your element is visible
}

<强> JAVASCRIPT

var display= document.getElementById('x').style.display;

if(display=='block')
 {
   //do validation here.
 }

答案 1 :(得分:1)

if( $('#Barcode').is(':visible') ){
    // Perform code here
}

How do I check if an element is hidden in jQuery?

if( $('#Barcode').is(':visible') && $('#Reason').val().length!==0 ){
    // Barcode is visible and reason has a value more then 0 chars long
}