[英]Passing a variable from PHP to javascript and on to an html form
[英]Passing a form variable from HTML to Javascript, but with quotes
我正在嘗試將電子郵件地址從HTML表單傳遞到javascript代碼段中,以傳遞給另一個程序。
表格輸入:
<input name="email" placeholder="Enter Email Address Here" type="email" required="required" id="email_input">
這是我要查找的示例,請注意引號。 為了使電子郵件能夠通過,它必須在引號內。
<script type="text/javascript">
test({
email: "example@example.com",
});
</script>
這是我嘗試過的:
<script type="text/javascript">
test({
email: "document.getElementById("email_input");",
});
</script>
如果我使用的是諸如example@example.com之類的靜態值,那么一切都會按預期進行。 我無法弄清楚如何將值從電子郵件輸入字段傳遞到“ email:” JavaScript並通過。
這里是我需要的:我那里有的document.getElementById(“ email_input”); 無法正常工作,那應該是什么?
我知道我在這里缺少一些簡單的東西,只是不確定是什么。
提前致謝!
如果您只是想使用該test
對象傳遞電子郵件輸入的值,則需要此。
var emailString = document.getElementById("email_input").value;
test({
email: emailString,
});
</script>
您想先獲取輸入的值,然后將其作為字符串傳遞(假設您需要第一個示例中的字符串)
有兩種方法可以從另一個typeof元素(DOM元素,Number,Array等)中獲取字符串:
<script type="text/javascript">
var elmt = document.getElementById("email_input");
var str1 = elmt.toString();
var str2 = elmt + "";
// alert(str1); and alert(str2) will pop the same result : a string of elmt
</script>
因此,如果要將整個輸入作為字符串,則應該執行以下操作:
<script type="text/javascript">
test({
email: document.getElementById("email_input") + ''
});
</script>
如果您只想將輸入值作為字符串(例如電子郵件),則應該執行以下操作:
<script type="text/javascript">
test({
email: document.getElementById("email_input").value + ''
});
</script>
如果確實需要在字符串中添加引號,也可以執行以下操作:
<script type="text/javascript">
var str= "this is my string";
var withquote= ' " ' + str + ' " ';
//both of the var return a string but one will have a quote and the other won't
alert(str); // pop: this is my string
alert(withquote); // pop "this is my string"
</script>
因此,在您的情況下:
如果要將整個輸入作為帶引號的字符串,則應該執行以下操作:
<script type="text/javascript">
test({
email: ' " ' + document.getElementById("email_input") + ' " '
});
</script>
如果您只想將輸入值作為帶引號的字符串(例如電子郵件),則應該執行以下操作:
<script type="text/javascript">
test({
email: ' " ' + document.getElementById("email_input").value + ' " '
});
</script>
希望我已經說清楚了,隨時問我是否不是
祝你有個好的一天
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.