简体   繁体   English

Javascript或jQuery:将输入值保存到变量中,该变量又保存到另一个变量中

[英]Javascript or jQuery: Saving input value into variable which is saved into another variable

I have been trying to save the value from an input into a variable which then is saved into another variable which holds a api call url, which is then tied to a getJSON function, for some reason I cannot seem to save the value of the input into the variable, it keeps replying back as blank, but in console it shows up as what the input value holds. 我一直在尝试将输入中的值保存到一个变量中,然后将其保存到另一个包含api调用网址的变量中,然后将其绑定到getJSON函数,由于某种原因,我似乎无法保存输入的值进入变量后,它会一直保持为空白,但在控制台中会显示为输入值所在。 What am I doing wrong? 我究竟做错了什么?

Update: Below is the image of the url not including the saved variable. 更新:以下是网址图片,其中不包含已保存的变量。 It should show what I had input into the variable in the full url. 它应该显示我在完整URL中输入到变量中的内容。 As you can see login_name= is blank and so is login_password. 如您所见,login_name =为空白,login_password也为空白。

在此处输入图片说明

jsFiddle: https://jsfiddle.net/m25pLka5/ jsFiddle: https ://jsfiddle.net/m25pLka5/

HTML: HTML:

<form id="sso-login">
  <table>
    <tr>
      <td>User Name:</td>
      <td><input class="username" name=login_name type=text size=15 maxlength="100"></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input class="password" name=login_password type=password size=15 maxlength="100"></td>
    </tr>
    <tr>
      <td>Remember Me:</td>
      <td><input name=remember_me type=checkbox></td>
    </tr>
  </table>
  <input type="submit" value="Submit" name="Submit" />
</form>

jQuery: jQuery的:

$(function(){
  var username = $('.username').val();
  var password = $('.password').val();

  var ssoURL = 'https://testing.com/';
  var ssoMethod = 'testingMethod&login_name='+ username +'&login_password='+ password +'&v=1.0&response_format=json';

  $("#sso-login").submit(function(e) {
    e.preventDefault();
    $.getJSON(ssoURL + ssoMethod, function(data) {
      console.log(data);
    });
  });
});

try changin ur code to somewhat 尝试将您的代码更改为

$(function(){  
  $("#sso-login").submit(function(e) {

   var username = $('.username').val();
  var password = $('.password').val();

  var ssoURL = 'https://testing.com?';
  var ssoMethod = 'testingMethod&login_name='+ username +'&login_password='+ password +'&v=1.0&response_format=json';
    e.preventDefault();
    $.getJSON(ssoURL + ssoMethod, function(data) {
      console.log(data);
    });
  });
});

and instead of using class to get the vals from element try using ids sometimes if the class is been used somewhere else in the dom (before the inputs) may clash so use ids as possible 而不是使用类从元素获取vals,有时,如果类在dom中的其他地方(在输入之前)使用了,则尝试使用id可能会发生冲突,因此请尽可能使用id

and also i guess ur apis url was wrong when it comes to params params start after ? 而且我也猜想您的API网址在参数设置方面是错误的吗?

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

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