सबमिट करें मैं node.js और एक्सप्रेस का उपयोग करता हूं। जब मैं बटन दबाता हूं (btnSend), मैं एक्सप्रेस द्वारा node.js को डेटा भेजना चाहता हूं (पृष्ठ को रीफ्रेश किए बिना)। मैं डेटा कैसे भेजूं? Jquery का उपयोग करना?Node.js - फॉर्म
कृपया मदद करें।
सलाह के लिए धन्यवाद।
$("form").submit(function(e) {
e.preventDefault(); // Prevents the page from refreshing
var $this = $(this); // `this` refers to the current form element
$.post(
$this.attr("action"), // Gets the URL to sent the post to
$this.serialize(), // Serializes form data in standard format
function(data) { /** code to handle response **/ },
"json" // The format the response should be in
);
});
यह कोड स्निपेट पृष्ठ पर सभी प्रपत्र तत्वों पाता है और उन लोगों से एक प्रस्तुत घटना के लिए सुनता है:
<form action="/Send" method="post">
Username:
<input type="text" name="user" id="txtUser" />
<input type="submit" value="Submit" id="btnSend" />
</form>
यह विषय बंद है, लेकिन एक "लेबल टैग] (https://developer.mozilla.org/en/HTML/Element/label) के साथ टेक्स्ट इनपुट तत्व को लपेटने पर विचार करें। यह फ़ॉर्म को थोड़ा अधिक अर्थपूर्ण और प्रयोग योग्य बनाता है। – Xavi