2014-03-27 50 views
2

我相信這是一個簡單的修復,但是在研究之後我還沒有弄清楚。Jquery:清除彙總表輸入值

我有一個多頁輸入表單,它顯示輸入字段中數據的摘要頁面。輸入字段有一個描述輸入字段的默認字符串,基本上是'id'屬性。當用戶點擊輸入字段輸入數據時,默認數據將消失,並輸入新數據。如果用戶不輸入輸入字段,則默認數據保持原樣。摘要頁面顯示錶單域中的所有輸入,包括默認數據。我遇到的問題是確定在顯示摘要頁面之前如何清除默認數據。我敢肯定,這很簡單,我忽略了,但我需要另一套銳利的眼睛和jquery敏銳。

摘要窗體有兩個並排嵌入div的表格。我正在使用nth-child()方法來選擇正確的td來顯示數據。

僞代碼邏輯:「如果一個輸入字段包含attr('id'),則清除該字段;否則,顯示該字段 。

我已經使用.val(""),.val(),.text("").html("")清除該字段無濟於事。我需要一些專業幫助。 :-)

//prepare the fourth step 
    var fieldsLeft = new Array(
     $('#firstname').val(), 
     $('#middlename').val(), 
     $('#lastname').val(), 
     $('#address').val(), 
     $('#city').val(), 
     $('#zip').val(), 
     $('#dob').val(), 
     $('#ssn').val() 
    ) 

    var fieldsRight = new Array(
     $('#homephone').val(), 
     $('#workphone').val(), 
     $('#cellphone').val(), 
     $('#employer').val(), 
     $('#emergency').val(), 
     $('#relationship').val(), 
     $('#phoneday').val(), 
     $('#phonenight').val(), 
     $('#email').val() 
     ); 

    var sumLeft = $('#fourth_step #leftSummary tr'); 
    sumLeft.each(function() { 
     var valueL = $(this).val(); 
     if (valueL == fieldsLeft[$(this).attr('id')]) { 
      $(this).children('td:nth-child(2)').val(''); 
     } else { 
      $(this).children('td:nth-child(2)').html(fieldsLeft[$(this).index()]) 
     } 
    }); 

    var sumRight = $('#fourth_step #rightSummary tr'); 
    sumRight.each(function() { 
     var valueR = $(this).val(); 
     if (valueR == fieldsRight[$(this).attr('id')]) { 
      $(this).children('td:nth-child(2)').val(''); 
     } else { 
      $(this).children('td:nth-child(2)').html(fieldsRight[$(this).index()]) 
     } 
    }); 
+1

嘿,這將是有用的,看到HTML(也許在一個jsfiddle),也是,你是什麼意思準確地與「清除領域」? – DarkAjax

+0

我想過使用jsfiddle ...爲什麼我不要繼續做下去。給我幾個。 – brohjoe

+0

哦,是的,通過'清除字段',我基本上是說把字段留空以免它出現在摘要頁面中。 – brohjoe

回答

0

我得到它使用佔位符。正如我所希望的那樣,當提交摘要視圖的表單時,輸入字段中的默認文本消失。

0

如果.hide()作品你想要的東西,你可以嘗試這樣的事:

//prepare the fourth step 
var fieldsLeft = 
$('#firstname, #middlename, #lastname, #address, #city, #zip, #dob, #ssn').map(function(){ 
    return $(this).val(); 
}); 

var fieldsRight = 
$('#homephone,#workphone,#cellphone,#employer,#emergency,#relationship,#phoneday,#phonenight,#email').map(function(){ 
    return $(this).val(); 
}); 

var sumLeft = $('#fourth_step #leftSummary tr'); 
sumLeft.each(function() { 
    var valueL = $(this).val(); 
    if (valueL == fieldsLeft[$(this).attr('id')]) { 
     $(this).children('td:nth-child(2)').hide(); 
    } else { 
     $(this).children('td:nth-child(2)').show(); 
    } 
}); 

var sumRight = $('#fourth_step #rightSummary tr'); 
sumRight.each(function() { 
    var valueR = $(this).val(); 
    if (valueR == fieldsRight[$(this).attr('id')]) { 
     $(this).children('td:nth-child(2)').hide(); 
    } else { 
     $(this).children('td:nth-child(2)').show(); 
    } 
}); 

此外,我建議的.map()的使用使陣列(假設你的投入都在與當前陣列相同的順序)

+0

好的,讓我試試你的解決方案darkajax。回來。 – brohjoe

+0

@brohjoe好的,只是檢查出來,因爲不知怎的,並不是所有的代碼都出現了,我只是編輯它... – DarkAjax

+0

顯然,它仍然無法正常工作。 '.hide()'方法隱藏了所有的字段。摘要是空白的。 – brohjoe