我有一个项目列表。我希望能够点击一个按钮将项目添加到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