简体   繁体   English

如何使用java脚本从函数内部获取值?

[英]how can i get a value from inside a function using java script?

here is the function from inside a script 这是脚本内部的功能

function dosubmit()
  {
    if (getObj("Frm_Username").value == "")
    {
      getObj("errmsg").innerHTML = "Username cannot be empty.";
      getObj("myLayer").style.visibility = "visible" ;
      return;
    }
    else
    {
      getObj("LoginId").disabled = true;
      getObj("Frm_Logintoken").value = "3";
      document.fLogin.submit();
    }
  }

i want to get the value of getObj("Frm_Logintoken") as i can't pull the value from #Frm_Logintoken 我想得到getObj("Frm_Logintoken")的值,因为我无法从#Frm_Logintoken提取值

using document.getElementById("#Frm_Logintoken") this gives me null 使用document.getElementById("#Frm_Logintoken")这给了我null

because Frm_Logintoken only gets it's value when i click submit . 因为当我点击提交时,Frm_Logintoken只获取它的值。

<input type="hidden" name="Frm_Logintoken" id="Frm_Logintoken" value="">

full page code 页代码

i found this online /getObj\\("Frm_Logintoken"\\).value = "(.*)";/g but when i run it ... it gives me the same line again ! 我发现这个在线/ /getObj\\("Frm_Logintoken"\\).value = "(.*)";/g但是当我运行它时...它再次给了我同样的线! it's full code https://hastebin.com/gurosatuna.xml 它的完整代码是https://hastebin.com/gurosatuna.xml

First: 第一:

Your checking if a value is empty with JS. 您使用JS检查值是否为空。 However this is NOT needed as HTML does this for you. 但是,这不是必需的,因为HTML会为您执行此操作。 Add a attribute required and the form will not submit as long this value is empty 添加所需的属性,只要此值为空,表单就不会提交

Documentation: https://www.w3schools.com/tags/att_input_required.asp 文档: https//www.w3schools.com/tags/att_input_required.asp

Second: 第二:

You could use the event handler 'on submit'. 您可以在提交时使用事件处理程序。 The code is not complete enough to know if u did this but I suppose you just added a Click handler on the button. 代码不够完整,不知道你是否这样做,但我想你刚刚在按钮上添加了一个Click处理程序。

Documentation: https://www.w3schools.com/jsref/event_onsubmit.asp 文档: https//www.w3schools.com/jsref/event_onsubmit.asp

When combining these two, you always have a username filled in and the code only executes when submitted. 组合这两个时,您总是填写用户名,代码仅在提交时执行。 I hope this helps, if not please leave a comment and I will edit this answer. 我希望这有帮助,如果没有请发表评论,我将编辑这个答案。

EDIT: the answer on this SO will also help (not the checked on but the one below) 编辑:这个SO的答案也会有所帮助(不是检查,而是下面的那个)

How can I listen to the form submit event in javascript? 如何在javascript中收听表单提交事件?

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

相关问题 我如何从 java 脚本中的 URL 获得价值 - How can i get value from URL in java script 如何在函数内获取此变量的值? - How can I get the value from this variable inside function? 如何使用 JAVA 从 SessionStorage 获取值 - How Can i get the value from SessionStorage using JAVA 如何使用Java脚本函数从使用SWT浏览器从Ajax请求获取数据的Eclipse函数获取返回值? - How to get return value to eclipse function from java script function which get data from ajax request using SWT browser? 使用Java脚本:如何更改内容替换功能以获得所需的结果? - Using Java script :How can I change my content replace function to get my desire result? 如何使用ASP.Net在Java脚本文件中获取另一个Form会话值? - How can i Get a Another Form Session Value in Java Script File Using ASP.Net? 如何监视一个函数并使用茉莉花从另一个函数内部返回模拟值? - How can I spy on a function and return a mock value from inside another function using jasmine? 如果它调用Selenium中的Java脚本函数,如何从Web元素获取文件URL - How can i get the file URL from web element if it calls java script function in Selenium 如何在jsp中获取Javascript函数的值? - How can i get value of Javascript function, inside jsp? 如何将函数内部变量的值获取到 on jquery 外部 - how can i get the value of variable inside function to outside the on jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM