简体   繁体   English

通过jQuery将输入值设置为var

[英]Setting input value to a var through jquery

i'm trying to set an input value to a var named account after the user hits a button, I've tried this without jquery and everything checks out fine, but this is for an assignment and I must use jquery. 我试图在用户单击按钮后将输入值设置为var命名帐户,我已经尝试过在没有jquery的情况下进行检查,并且一切都很好,但这是针对赋值的,我必须使用jquery。

here is the html input and button: 这是html输入和按钮:

<input type="" class="Re" id="Email1"/>
<button class="Re" id="Start" >Start</button>

Now i set the button to show an alert with the input value in the var account to check if it works, here is the code: 现在,我将按钮设置为在var帐户中显示带有输入值的警报,以检查其是否有效,这是代码:

var account = "a";
$(document).ready(function(){
    $("#Start").click(function(){
        $("#Email1").val(account);
        alert(account);
    });
});

account it's set on "a" mainly for testing, the "a" doesn't seem to change nothing. 帐户将其设置为主要用于测试的“ a”,“ a”似乎并没有改变。

If you want to set the value of the account variable to the form field's value, call $.val() without arguments: 如果要将account变量的值设置为表单字段的值,请调用不带参数的$ .val():

var account = "a";
$(document).ready(function(){
    $("#Start").click(function(){
        account = $("#Email1").val();
        alert(account);
    });
});

You just need to set account to $("#Email1").val(); 您只需要将帐户设置为$(“#Email1”)。val();

 var account = "a"; $(document).ready(function() { $("#Start").click(function() { account = $("#Email1").val(); alert(account); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="" class="Re" id="Email1" /> <button class="Re" id="Start">Start</button> 

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

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