简体   繁体   English

如何获取输入元素的值而不是“null”?

[英]How can I get the value of an input element instead of `null`?

When the user inputs any number, the number needs to be joined with the assigned text (in this case a URL).当用户输入任何数字时,该数字需要与分配的文本(在本例中为 URL)相结合。 However it results in this: http://someurl.com/?value=null .然而,结果是: http://someurl.com/?value=null Instead of null I expect the number the user inputted.我希望用户输入的数字不是null

<head>
  <script type="text/javascript">
    var juarel = "http://someurl.com/?value=";
    var velju = document.getElementById('somenumber');
    var rezon = juarel.concat(velju);

    function print() {
      alert(rezon);
    }
  </script>
</head>
<body>
  <form>
    <input type="number" id="somenumber" value="">
    <button type="button" id="submit" name="button" onclick="print()">SUBMIT</button>
  </form>
</body>

Get the correct value from the HTML element calling .value .从调用.value的 HTML 元素中获取正确的值。 Then put the code to retrieve the value inside the function, otherwise you are reading the value at the beginning, and the element is empty.然后把取值的代码放在函数里面,否则你是在开头读取值,元素为空。 You have to read the value when you press the button:当您按下按钮时,您必须读取该值:

 <body> <form> <input type="number" id="somenumber" value=""> <button type="button" id="submit" name="button" onclick="print()">SUBMIT</button> </form> <script type="text/javascript"> function print () { var juarel = "http://someurl.com/?value="; var velju = document.getElementById('somenumber').value; var rezon = juarel.concat(velju); alert(rezon); } </script> </body>

1st use .value as var velju = document.getElementById('somenumber').value第一次使用.value作为var velju = document.getElementById('somenumber').value

2nd wrap those into the function, so when you click, you get the current value, otherwise, it is always undefined since it is called on init. 2nd 将它们包装到函数中,所以当你点击时,你会得到当前值,否则,它总是未定义的,因为它在 init 上被调用。

  function print() {
    var juarel = "http://someurl.com/?value=";
    var velju = document.getElementById('somenumber').value;
    var rezon = juarel.concat(velju);
    alert(rezon);
  }

 <script type="text/javascript"> function print() { var juarel = "http://someurl.com/?value="; var velju = document.getElementById('somenumber').value; var rezon = juarel.concat(velju); alert(rezon); } </script> <body> <form> <input type="number" id="somenumber" value=""> <button type="button" id="submit" name="button" onclick="print()">SUBMIT</button> </form>

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

相关问题 如何获得我的复选框而不是 Null 的正确值? - How can I get the proper value of my checkbox instead of Null? 如何获取同级输入元素的值? - How can I get the value of a sibling input element? 如何从残疾人那里获取表单值<input>元素 - How can I get the form value from a disabled <input> element 如何从量角器中的输入元素而不是承诺中获取文本值 - How to get as text value from input element in protractor instead of promise 使用“共享”按钮(而不是“提交”类型的输入元素)时,如何使表单验证起作用? - How can I get Form Validation to work when using a “shared” button (instead of an input element of type “submit”)? foreach的结果是数组,如何获取元素的值而不是数组? - foreach results in an array, how can I get the value of element instead of an array? 输入为“ null”。 如何计算带日期的输入值? - Input is `null`. How can I calculate the input value with date? 我如何让 inputRef 返回<input>元素来自<Textfield> ? 尝试了我能想到的一切,它仍然为空 - How do I get inputRef to return <input> element from <Textfield>? Tried everything I can think of and it's still null 如何获取输入的“文本”值? - How can I get the Text value of input? 如何获取输入数组的值 - How can I get value of input array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM