2016-03-01 48 views
1

您好,我在檢索輸入字段的文本值時遇到問題。兩個輸入字段隱藏在表格行內,只有在選擇元素中選擇特定選項時纔會顯示。我不確定是否因某種原因影響訪問輸入字段數據。下面的函數顯示瞭如何根據select選項的值顯示和隱藏表格行。無法使用普通的JQuery/Javascript檢索輸入字段的值

我試圖獲取值都正常jQuery的方式和JavaScript的方式:

$('#doc_reference').attr('value'); document.getElementById('doc_reference').value 

但要麼沒有運氣,誰能幫助?

//toggle doc_reference and y_reference dependent on modelling language chosen 
    $("#langId").change(function() { 
    var val = $(this).val(); 
    console.log(val); 
    if(val == 1) { 
     $('.hideShowTr').hide(); 
    } 
    else if(val == 2) { 
     $('.hideShowTr').show(); 
    } 
    }); 

var docReference =$('#doc_reference').attr('value'); 
var yReference = $('#y_reference').attr('value');  

<tr class = 'hideShowTr' style='display: none;'> 
    <td> 
     <span style='display:block; position:relative; padding:0; z-index:0;'> 
      <input type='text' name='doc_reference' id='doc_reference' style='position:absolute; width:195px; z-index:151;'></input> 
     </span> 
    </td> 
</tr> 

<tr class = 'hideShowTr' style='display: none;'> 
    <td> 
     <span style='display:block; position:relative; padding:0; z-index:0;'> 
      <input type='text' name='y_reference' id='y_reference' style='position:absolute; width: 195px; top:2px; z-index:151;'></input> 
     </span> 
    </td> 
</tr> 
+0

當你得到的價值?它看起來像你需要得到'val()'一旦他們被更新? –

回答

3

►您不要在你的元素屬性值。

使用這段代碼來檢索值。

$('#doc_reference').val(); 

DEMO

$("#doc_reference,#y_reference").keyup(function() { 
 
    alert($(this).val()) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<tr class='hideShowTr'> 
 
    <td> 
 
    <span style='display:block; position:relative; padding:0; z-index:0;'> 
 
      <input type='text' name='y_reference' id='y_reference' style='position:absolute; width: 195px; top:2px; z-index:151;'> 
 
     </span> 
 
    </td> 
 
</tr>

+0

這很好,謝謝! – olliejjc16