2012-01-16 8 views
18

मैं टेक्स्ट बॉक्स से टेक्स्ट प्राप्त करने का प्रयास कर रहा हूं।टेक्स्ट बॉक्स से इनपुट मान प्राप्त करना

मेरे पास 2 इनपुट टेक्स्ट बॉक्स हैं जो एक रूप में नहीं हैं, और मैं value पुनर्प्राप्त करने की कोशिश कर रहा हूं और इसे एक चर में संग्रहीत कर रहा हूं।

यह कोड पॉप-अप होने वाले अलर्ट बॉक्स में undefined देता है।

<script> 
    var userPass = document.getElementById('pass'); 
    var userName = document.getElementById('fName'); 
    function submit(){ 
     alert(userPass.value); 
    } 
</script> 

जब मैं चेतावनी समारोह में पैरामीटर के रूप userName.value के साथ चलाने के लिए, यह काम करते हैं और प्रदर्शित क्या कभी आप बॉक्स में टाइप होगा।

<table id="login"> 
     <tr> 
      <td><label>User Name</label></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input class="textBox" id="fName" type="text" maxlength="30" required/></td> 
     </tr> 
     <tr> 
      <td id="pass"><label>Password</label></td> 
     <tr> 
      <td colspan="2"><input class="textBox" id="pass" type="text" maxlength="30" required/></td> 
     </tr> 
     <tr> 
      <td><input type="button" class="loginButtons" value="Login" onclick="submit();"/>&nbsp&nbsp&nbsp 
      <input type="button" class="loginButtons" value="Cancel"/></td> 
    </table> 
+0

<इनपुट वर्ग = "टेक्स्टबॉक्स" आईडी = "पास" प्रकार = "पाठ" maxlength = "30" आवश्यक /> – Beanno1116

+0

आपका प्रश्न क्या है? – blake305

+0

प्रासंगिक एचटीएमएल –

उत्तर

6

आप देखेंगे कि आपके पास input टैग में कोई मूल्य नहीं है।
इसके अलावा, हालांकि नहीं दिखाया गया है, सुनिश्चित करें कि जावास्क्रिप्ट के बाद एचटीएमएल चल रहा है।

+0

की सराहना करें मेरे पास मेरे इनपुट तत्व में डिफ़ॉल्ट मान होना चाहिए? – Beanno1116

+0

इसे आज़माएं, मुझे लगता है कि प्रत्येक ब्राउज़र इस संबंध में थोड़ा अलग है। –

2

आप एक ही आईडी के साथ कई तत्व होते हैं:

यहाँ एचटीएमएल है। यह एक बड़ा नो-नो है। सुनिश्चित करें कि आपके इनपुट में अद्वितीय आईडी हैं।

<td id="pass"><label>Password</label></td> 
<tr> 
    <td colspan="2"><input class="textBox" id="pass" type="text" maxlength="30" required/></td> 
</tr> 

दोनों td और input आईडी मान pass का हिस्सा दिखाई देता है तो।

+0

हाँ मैंने देखा कि धन्यवाद। मुझे यह भी एहसास नहीं हुआ कि मैंने नाम दिया है। सहायता – Beanno1116

1

तत्व से id="pass" हटाएं। अभी जेएस इनपुट के बजाय टीडी तत्व प्राप्त करेगा इसलिए मूल्य अपरिभाषित है।

+0

धन्यवाद कि यह वही था। मदद की सराहना करें – Beanno1116

5

<script> 
    function submit(){ 
     var userPass = document.getElementById('pass'); 
     var userName = document.getElementById('user'); 
     alert(user.value); 
     alert(pass.value); 
    } 
</script> 

<input type="text" id="user" /> 
<input type="text" id="pass" /> 
<button onclick="submit();" href="javascript:;">Submit</button> 

0

यह ईमेल और जावास्क्रिप्ट के लिए नमूना कोड है।

params = getParams(); 
subject = "ULM Query of: "; 
subject += unescape(params["FormsEditField3"]); 
content = "Email: "; 
content += unescape(params["FormsMultiLine2"]); 
content += "  Query: "; 
content += unescape(params["FormsMultiLine4"]); 
var email = "[email protected]"; 
document.write('<a href="mailto:'+email+'?subject='+subject+'&body='+content+'">SUBMIT QUERY</a>'); 
4

// नोट: "this.pass" और का उपयोग करना "this.name" एक वैश्विक चर भले ही यह समारोह के अंदर है बन जाएगा, ताकि आपके नामकरण परंपरा के थके हुए हो

function submit() 
{ 
    var userPass = document.getElementById("pass").value; 
    var userName = document.getElementById("user").value; 
    this.pass = userPass; 
    this.name = userName; 
    alert("whatever you want to display"); 
} 
1

जावास्क्रिप्ट दस्तावेज़ .getElementById ("<% = contrilid.ClientID%>")। मूल्य; या jquery

$ ("# <% = txt_iplength.ClientID%>") का उपयोग कर। val();