我有一個項目列表。我希望能夠點擊一個按鈕將項目添加到mySQL。在測試時,我只需使用conf box來顯示ID。麻煩是我無法讓它工作。我是jQuery和AJAX的新手。我認爲這很容易。但經過一天早上從這裏嘗試了一堆不同的方法後,我想我會問我的確切代碼。jQuery SelectButton旁邊的隱藏輸入值
<page loaded with>
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_script('jquery-ui-autocomplete');
<script>
function addCharItem() {
var itemID = $(this).closest("td").find('input[name="itemID"]').val(); //this is close (I think) but message is showing as undefined.
// var itemID = $("input").siblings(".itemID").val() //gets first ID in table only
confirm ('The item ID is ' + itemID);
//adding ajax and other stuff later.
}
</script>
<html>
...
<tr>
<td colspan="5"><dl><dt>Backpack (empty)</dt><dd></dd></dl></td>
<td><input name="hdnItemID" type="hidden" value="742"/>
<input name="btnAddItem" type="button" id="btnAddItem" value="+" onclick="addCharItem()"/></td>
<td><input name="btnBuyItem" type="button" id="btnBuyItem" value="$" /></td>
</tr>
...
</html>
這是全部從Wordpress中的模板php頁面構建而成。我不認爲這些東西有什麼奇怪之處。但如果讓我知道你還有什麼需要的。感謝您的幫助和建議。我知道我必須錯過一些小小的東西,我只是不知道這個東西足夠好,還不知道是什麼。
到目前爲止沒有運氣。我添加了以下內容:\t var thisValue = $(this); console.log(thisValue);看看這是什麼選擇。它在控制檯中返回:[Window,jquery:「1.9.1」,constructor:function,init:function,selector:「」,size:function ...] 它不應該有選擇器嗎?它顯示「」。也許這就是問題所在。爲什麼'this'不能抓住當前的元素呢? – user1518699