2017-04-19 37 views
-2

我在清除html表單輸入時遇到了問題。 這是我的情況:使用javascript清除兩個輸入

<input id="filefield" type="file" name="fileupload"/>    
<textarea id="textareafield" name="textupload"></textarea> 

    <script> 
     ('#textareafield').change(function() { 
     value1 = $(this).val(); 
     if (value1 != '') $('#filefield').val(''); 
    }); 

     ('#filefield').change(function() { 
     value2 = $(this).val(); 
     if (value2 != '') $('#textareafield').val(''); 
    }); 
    </script> 

如果#filefield使用刪除#textareafield的內容,如果#textareafield用於刪除#filefield內容。我需要一個與JavaScript或jQuery的解決方案。謝謝。

+2

'(...)'只是羣體的表現。在連接變更處理程序的代碼上,您可能需要'$(...)',它調用由'$'標識符引用的jQuery函數,就像稍後查找字段以清除其值時一樣。 –

+0

在Web開發中,要做的關鍵事情之一是在Web控制檯中查看。在這種情況下,它會顯示錯誤'Uncaught TypeError:「#textareafield」.change不是一個函數或類似的,指出問題... –

回答

0
$(function() { 
    console.log("ready"); 
    $("#filefield").change(function() { 
     if ($(this).val() != '') { 
      $('#textareafield').val(''); 
      // make this field "readonly" 
      $('#textareafield').attr('readonly', true); 
     } 
     else { 
      $('#textareafield').attr('readonly', false); 
     } 
    }); 

    $("#textareafield").change(function() { 
     if ($(this).val() != '') { 
      $('#filefield').val(''); 
      // disable the "file" button 
      $('#filefield').attr('disabled', true); 
     } 
     else { 
      // enable the "file" button 
      $('#filefield').attr('disabled', false); 
     } 
    }); 
}); 

爲例上的jsfiddle:https://jsfiddle.net/w3z17dvb/