0

如果行中的其他单元格之一具有值,我正在尝试获取表行中一个输入的值。这里的目标是,如果用户在行中输入收到的钱,那么需要一个收到日期。因此,在提交时,我想查看所有行并检查当前单元格中的条目,然后检查日期单元格中的日期。付款条目不止一行。这是HTML:

       <div class="new_payments">
        <tr>
            <td>
                <input type="text" 
                name="new_payments[<?php echo $s ?>][payment_date]"
                class="payment_date" 
                size='10' maxlength="10" 
                > 
            </td>                                
            <td>                            
                <input type="text" 
                name="new_payments[<?php echo $s ?>][credit_card]"
                class="credit_received"                               
                size="8">
            </td>  
            <td>
                <input type="text" 
                name="new_payments[<?php echo $s ?>][check]"
                class="check_received" 
                size="8">            
            </td>
            <td>
                <input type="text"  
                name="new_payments[<?php echo $s ?>][cash]"
                class="cash_received" 
                size="8">             
            </td>
        </tr>
    </div>

这是我正在使用的 jQuery(尚未添加对 'this' 中的值的检查),以查看我是否得到了预期的结果。可悲的是,我得到“未定义”:

    jQuery('input:text.cash_received').each (function() {
       var $theDate  = jQuery(this).closest('div.new_payments').find('.payment_date').attr('value');
       alert('the closest date value is ' + $theDate );
}); 

我想我理解了去父级然后找到你正在寻找的元素的概念。显然不是?感谢您的出色帮助!

4

1 回答 1

0

这是经过测试和工作的。它只会在.cash_received有值时发出警报。我在输入中添加了值以进行测试。还要注意 div 包装器的删除。我将课程添加到 TR

jQuery(document).ready(function(){
    jQuery('input[type=text][value!=""].cash_received').each (function() {
        var myval= jQuery(this).parents('.new_payments').find('.payment_date').val();
           alert('the closest date value is ' +myval);
    });
});



    <tr class="new_payments">
        <td>
            <input type="text" 
            name="new_payments[<?php echo $s ?>][payment_date]"
            class="payment_date" value='this should alert'
            size='10' maxlength="10" 
            > 
        </td>                                
        <td>                            
            <input type="text" 
            name="new_payments[<?php echo $s ?>][credit_card]"
            class="credit_received"                               
            size="8">
        </td>  
        <td>
            <input type="text" 
            name="new_payments[<?php echo $s ?>][check]"
            class="check_received"
            size="8">            
        </td>
        <td>
            <input type="text"  
            name="new_payments[<?php echo $s ?>][cash]"
            class="cash_received" value='this input has a value'
            size="8">             
        </td>
    </tr>
于 2012-01-13T16:40:07.327 回答