2013-10-10 49 views
0

如果jQuery的文本框爲空值

alert($(TheButton).parent().parent().children("td").html()); 

回報

<input id="Quantity" name="Quantity" 
style="width: 35px; 
background-color: rgb(254, 254, 254);" type="text" value="1"> 

爲什麼

alert($(TheButton).parent().parent().children("td").val()); 

返回一個空字符串?

+1

的'.parent()。父()。兒童( 「TD」)'顯得過於硬編碼..如果您發佈的HTML,我相信我們可以改善這個選擇.. –

+0

謝謝你的幫助!你是個好人。 – Pinch

回答

4

因爲td元素沒有值。

當你打電話給.children('td')你得到的是一個td元素的列表。當你打電話給.html()你所得到的是裏面的HTML 這個元素。在這種情況下,HTML恰巧是一個input元素。但.html()並沒有看出,它只是返回那裏。

所以在這種情況下,當你打電話給.children('td').val()你打電話.val()td元素,它沒有值。也許你打算選擇input元素呢?

alert($(TheButton).parent().parent().children("td").find("input").val()); 
+0

你是正確的,謝謝! - 那警報把羊的眼睛放在了上面。 – Pinch

相關問題