我使用它来获取输入
的值并将其放入页面末尾的另一个输入中:
jQuery( document ).ready(function() {
jQuery('.free-lesson input[type="text"]').each(function () {
jQuery(".free-lesson input[type='submit']").click(function(e) {
e.preventDefault();
var freeLessonContent = jQuery(this).val();
jQuery("#mce-EMAIL").val(freeLessonContent);
});
});
});
有很多 .free-lesson
div,但奇怪的是只有最后一个有效(其他的没有为表单中的 input
赋予任何值。
如何解决这个问题?
最佳答案
您不需要在其中添加foreach
。您在 foreach
中引用的 this
不是您需要的元素。试试这个。
jQuery('.free-lesson-form [type="submit"]').click(function(e){
e.preventDefault();
jQuery('#mce-EMAIL').val(jQuery(this).prev().val()); //find the element previous to the one that was clicked.
//You may also want to use .parent() and find the input as this is more reliable if you move the markup around
});
我在这里所做的是绑定(bind)到 free-lesson-form
div 内所有提交的点击事件。然后捕获所单击的按钮旁边的文本输入的值。在您的网站上尝试过这一点,它适用于当前的标记。
关于javascript - 即使我使用了 .each,为什么只有一个元素会触发该操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26814187/