我有一份物品清单。我希望能够单击一个按钮将项目添加到mySQL。在测试的时候,我只有一个conf框来显示ID,问题是我无法让它工作。我是jQuery和AJAX的新手。我觉得这很容易。但是,经过一天早上的尝试,在没有运气的情况下尝试了一系列不同的方法,我想我应该问一下我的确切代码。
<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页面构建的。我不认为这些东西有什么奇怪之处。但如果有,告诉我你还需要什么。谢谢你的帮助和建议。我知道我一定是错过了一些很小的东西,我只是还不知道该怎么做。
发布于 2013-10-30 20:46:01
因此,由于某种原因,上面的代码似乎没有抓住HTML元素。因此,我实现了jQuery按钮。
<script>
$(function() {
$( "input[type=submit], a, button" )
.button()
.click(function( event ) {
event.preventDefault();
var thisValue = $(this).val(); //to distinguish buttons
console.log(thisValue);
var itemID = $(this).prev('input[id="hdnItemID"]').val()
confirm ('The item ID is ' + itemID);
//do other stuff
});
});
</script>
<html changes>
...
<tr>
<td colspan="5"><dl><dt>Backpack (empty)</dt><dd></dd></dl></td>
<td><input class="itemID" id="hdnItemID" type="hidden" value="742"/>
<input type="submit" value="+" /></td>
<td><input class="itemID" id="hdnItemID" type="hidden" value="742"/>
<input type="submit" value="$" /></td></tr>
...
</html>我希望我能告诉你它为什么不起作用。看来我已经尝试了很多不同的方法。上述建议也应该有效。如果有人知道为什么会这样,请告诉我。我,我相信还有其他人,他们很想知道这个“抓到”的问题。谢谢你的小费!
发布于 2013-10-30 18:37:19
您可以使用.siblings(selector)
$(this).siblings('input[name="hdnItemID"]').val()或
$(this).prev('input[name="hdnItemID"]').val()发布于 2013-10-30 18:37:45
使用hdnItemID作为您的名称,而不是itemID
$(this).closest("td").find('input[name="hdnItemID"]').val();https://stackoverflow.com/questions/19691047
复制相似问题