2015-05-04 45 views
1

我有3個文本框在html「名稱,電子郵件,評論」,我想打印在控制檯作爲對象使用jquery,但它不工作。在控制檯中打印表單數據作爲對象使用jquery

Object {name: "Ali", email: "[email protected]", message: "hi there"}

<form action="jq_ajax_contact.php" class="ajax" method="post"> 
     <div><input type="text" name="name" placeholder="Your name"></div> 
     <div><input type="text" name="email" placeholder="Your email"></div> 
     <div><textarea name="message" placeholder="Your messsage"></textarea></div> 
     <div><input type="submit" value="send"></div> 
    </form> 
+0

$( 'form.ajax')。在( '提交',函數(){VAR 即= $(本), URL = that.attr( '行動') , method = that.attr(method), data = {}; $(this).find('[name]')。each(function(index,value){var} = $(this), name = that.attr('name'), value = that .val(); data [name] = value; }); console.log(data); 返回false; }); – mohsin

回答

1

的jsfiddle:http://jsfiddle.net/vLgxdvu4/

$('form').submit(printObj); 

function printObj(e) { 
    e.preventDefault(); 
    var obj = { 
    name: $('input[name=name]').val(), 
    email: $('input[name=email]').val(), 
    message: $('textarea[name=message]').val(), 
    } 
    console.log(obj); 
} 

HTML無形體動作提交(響應評論)

<form class="ajax" > 
     <div><input type="text" name="name" placeholder="Your name"></div> 
     <div><input type="text" name="email" placeholder="Your email"></div> 
     <div><textarea name="message" placeholder="Your messsage"></textarea></div> 
     <div><input type="submit" value="send"></div> 
    </form> 
+0

它仍然沒有在控制檯上打印 – mohsin

+0

您是否可能讓您的控制檯僅顯示錯誤或警告? –

+0

不是它被選中全部在過濾器 – mohsin

-1

只要確保你擁有jQuery的文件正確的路徑在這個標籤<script type="text/javascript" src="jquery1.8.2.js"></script>如果你管理下面的g是完整的工作示例

<form class="ajax" > 
     <div><input type="text" name="name" placeholder="Your name"></div> 
     <div><input type="text" name="email" placeholder="Your email"></div> 
     <div><textarea name="message" placeholder="Your messsage"></textarea></div> 
     <div><input type="submit" value="send"></div> 
    </form> 



<script type="text/javascript" src="jquery1.8.2.js"></script> 
<script> 
$('form').submit(function(e){ 
e.preventDefault(); 
     var obj = { 
     name: $('input[name=name]').val(), 
     email: $('input[name=email]').val(), 
     message: $('textarea[name=message]').val() 
     } 
     console.log(obj); 

alert("printed in console"); 
    }); 

</script>