简体   繁体   English

将表单变量从HTML传递到Javascript,但带引号

[英]Passing a form variable from HTML to Javascript, but with quotes

I'm trying to pass an email address from an HTML form into a javascript snippet to pass to another program. 我正在尝试将电子邮件地址从HTML表单传递到javascript代码段中,以传递给另一个程序。

Form input: 表格输入:

<input name="email" placeholder="Enter Email Address Here" type="email" required="required" id="email_input">

Here's an example of what I'm looking for, note the quotes. 这是我要查找的示例,请注意引号。 In order for the email to pass, it must be within quotes. 为了使电子邮件能够通过,它必须在引号内。

<script type="text/javascript">
test({

  email: "example@example.com", 

});
</script>

Here's what I've tried: 这是我尝试过的:

<script type="text/javascript">
test({

  email: "document.getElementById("email_input");", 

});
</script>

If I'm using a static value like example@example.com, everything works as expected. 如果我使用的是诸如example@example.com之类的静态值,那么一切都会按预期进行。 I'm not able to figure out how to pass the value from the email input field to the "email:" javascript and have it pass though. 我无法弄清楚如何将值从电子邮件输入字段传递到“ email:” JavaScript并通过。

Heres what I need: What I have in there document.getElementById("email_input"); 这里是我需要的:我那里有的document.getElementById(“ email_input”); is not working, what should this be? 无法正常工作,那应该是什么?

I'm know I'm missing something simple here, just not sure what. 我知道我在这里缺少一些简单的东西,只是不确定是什么。

Thanks in advance! 提前致谢!

If you're just trying to pass the value of the email input using that test object, this is what you need. 如果您只是想使用该test对象传递电子邮件输入的值,则需要此。

var emailString = document.getElementById("email_input").value;
test({

  email: emailString, 

});
</script>

You want to grab the value of the input first, and then pass it as a string (assuming you need a string from your first example) 您想先获取输入的值,然后将其作为字符串传递(假设您需要第一个示例中的字符串)

There is two way to get a string from an other typeof element (DOM element, Number, Array, ...) : 有两种方法可以从另一个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>

so if you want the entire input as a string you should do : 因此,如果要将整个输入作为字符串,则应该执行以下操作:

<script type="text/javascript">
test({

  email: document.getElementById("email_input") + '' 

});
</script>

if you just want the input value as a string (so the email) you should do : 如果您只想将输入值作为字符串(例如电子邮件),则应该执行以下操作:

<script type="text/javascript">
test({

  email: document.getElementById("email_input").value + '' 

});
</script>

and if you really need to add quotes to a string you also can do : 如果确实需要在字符串中添加引号,也可以执行以下操作:

<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>

So in your case : 因此,在您的情况下:

if you want the entire input as a string with quotes you should do : 如果要将整个输入作为带引号的字符串,则应该执行以下操作:

<script type="text/javascript">
test({

  email: ' " ' + document.getElementById("email_input") + ' " ' 

});
</script>

if you just want the input value as a string (so the email) with quotes you should do : 如果您只想将输入值作为带引号的字符串(例如电子邮件),则应该执行以下操作:

<script type="text/javascript">
test({

  email: ' " ' + document.getElementById("email_input").value + ' " ' 

});
</script>

hope I've made myself clear Feel free to ask if I'm not 希望我已经说清楚了,随时问我是否不是

have a good one 祝你有个好的一天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM