2012-08-01 82 views
0

我用一個函數來清除容器中的表單字段的輸入值:如何給不同的輸入字段上鍊接不同的值,單擊

function clear_form_elements(ele) { 

$(ele).find(':input').each(function() { 
    switch(this.type) { 
     case 'password': 
     case 'select-multiple': 
     case 'select-one': 
     case 'text': 
     case 'textarea': 
      $(this).val(''); 
      break; 
     case 'checkbox': 
     case 'radio': 
      this.checked = false; 
    } 
});} 

這完全脫離一個div內一組輸入字段,字段集或類似的(定義爲ele)。現在,我試着用

if(this.id = "town") {this.val('town');} 
if(this.id = "country") {this.val('country');} 

更換

$(this).val(''); 

更換不同ID的輸入元素,但返回的值的每個輸入字段始終是「國家」,儘管該元素的ID是不同的。

我在做什麼錯?

+0

如果你的this.val有$(this).val()? – 2012-08-01 12:56:32

回答

2

變化:

if(this.id = "town") {this.val('town');} 
if(this.id = "country") {this.val('country');} 

if(this.id == "town") {this.val('town');} 
if(this.id == "country") {this.val('country');} 

(雙等於)

你的語句總是設定值後返回true。你試圖比較,而不是設置一個值。

+0

Total facepalm:D謝謝! – Lukas 2012-08-01 13:14:18

+0

發生在最好的伴侶身上。 – Rodik 2012-08-01 13:15:07

相關問題