我使用
JQuery提交表单.表格如下
<form class="ask-more-form"> <div class="product_info_2"> <textarea name="product_question" class="textBox" placeholder="Ask You Question Here"></textarea> </div> <input type="hidden" name="product_id" value="1" id="product_id"> <a href="#" class="whiteButton submit" id="ask-more-submit-button">Ask Question</a> </form>
而JQuery抓住窗体看起来像这样:
$('#ask-more').on('submit','.ask-more-form',function() { var product_id = $(this).children('input[name=product_id]').val(); var product_question = $(this).children('textarea[name="product_question"]').text(); alert(product_question); //submitQuestion(product_id,product_question); });
product_id始终被读取,但产品问题始终为空.任何人都可以告诉我为什么会发生这种情况?
解决方法
.children
only goes one level down.使用.find代替:
$('#ask-more').on('submit',function () { var product_id = $(this).children('input[name=product_id]').val(); var product_question = $(this).find('textarea[name="product_question"]').text(); alert(product_question); //submitQuestion(product_id,product_question); });