简体   繁体   中英

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

using document.getElementById("#Frm_Logintoken") this gives me null

because Frm_Logintoken only gets it's value when i click submit .

<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 ! it's full code https://hastebin.com/gurosatuna.xml

First:

Your checking if a value is empty with JS. However this is NOT needed as HTML does this for you. 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

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.

Documentation: 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)

How can I listen to the form submit event in javascript?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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