简体   繁体   English

将表单中的参数发送到 JavaScript 中的函数

[英]Sending a parameter from a form to a function in JavaScript

I'm trying to send a value from the form to the new window that opens.我正在尝试将值从表单发送到打开的新窗口。 In my program the window opens but the value val2 is not sent and instead null exists.在我的程序中,窗口打开,但没有发送值val2 ,而是存在 null。

My code:我的代码:

 function eq(value) { ViewImg = window.open('http://localhost/coord.php?' + value, 'ViewImg', 'width=<?php print $realWidthNewWindow; ?>,height=<?php print $realHeightNewWindow; ?>', 'status=no', 'titlebar=0'); }
 <form id='testform_eq' name='testform_new'> <input type='text' id='val2' name='val2'> <input type='button' value='Submit' onclick='eq(document.getElementById(val2))'> </form>

Does anyone have an idea why this is so?有谁知道为什么会这样? Thanks谢谢

 function eq() { const value = document.getElementById('val2').value; ViewImg = window.open('http://localhost/coord.php?' + value, 'ViewImg', 'width=<?php print $realWidthNewWindow; ?>,height=<?php print $realHeightNewWindow; ?>', 'status=no', 'titlebar=0'); } 
 <form id='testform_eq' name='testform_new'> <input type='text' id='val2' name='val2'> <input type='button' value="Submit" onclick='eq()'> </form> 

You don't need to pass anything to the click function. 您无需向click功能传递任何内容。 Since you already have an id attribute to your input element you can get the value of the input when you click it. 由于您的input元素已经有了id属性,因此单击它时可以获得输入值。 Probably you would wanna use that function in other places and that would mean that you would have to pass every time that element. 可能你会想在其他地方使用该功能,这意味着你必须每次都传递该元素。

If you really want to pass something you can pass the id itself. 如果你真的想传递一些东西,你可以传递id本身。

eq('val2')

The eq function would look like this than: eq函数看起来像这样:

function eq(id){
  const value = document.getElementById(id).value;
  ....
}

Than you can use that function in other places on other inputs. 您可以在其他输入的其他地方使用该功能。

Because you're supposed to do it like this: document.getElementById("val2").value 因为你应该这样做: document.getElementById("val2").value

document.getElementById(val2) is incorrect if you want to get the id=val2 value. 如果要获取id=val2值, document.getElementById(val2)不正确。

To fix it you can replace eq(document.getElementById(val2) into eq(document.getElementById("val2").value) 要修复它,你可以将eq(document.getElementById(val2)替换为eq(document.getElementById("val2").value)

 function eq(value) { ViewImg = window.open('http://localhost/coord.php?' + value, 'ViewImg', 'width=<?php print $realWidthNewWindow; ?>,height=<?php print $realHeightNewWindow; ?>', 'status=no', 'titlebar=0'); console.log(value); } 
 <form id='testform_eq' name='testform_new'> <input type='text' id='val2' name='val2'> <input type='button' value='Submit' onclick='eq(document.getElementById("val2").value)'> </form> 

or simply put the id in function call: eq(val2) 或者只是将id放在函数调用中: eq(val2)

 function eq(id) { ViewImg = window.open('http://localhost/coord.php?' + id.value, 'ViewImg', 'width=<?php print $realWidthNewWindow; ?>,height=<?php print $realHeightNewWindow; ?>', 'status=no', 'titlebar=0'); console.log(id.value); } 
 <form id='testform_eq' name='testform_new'> <input type='text' id='val2' name='val2'> <input type='button' value='Submit' onclick='eq(val2)'> </form> 

If you want to pass a value of an input, you should take into account two things: 如果要传递输入值,则应考虑两件事:

  1. getElementById accepts a string, but you're trying to pass a variable, which doesn't exist getElementById接受一个字符串,但是你试图传递一个不存在的变量
  2. If you want to pass a value and not an element, you should add .value after the element 如果要传递值而不是元素,则应在元素后添加.value

And finally you should have the input element like this: 最后你应该有这样的input元素:

<input type='button' value='Submit' onclick='eq(document.getElementById("val2").value)'>

Here is a live example: 这是一个实例:

 <script> function eq(value) { console.log(value); ViewImg = window.open('http://localhost/coord.php?' + value, 'ViewImg', 'width=<?php print $realWidthNewWindow; ?>,height=<?php print $realHeightNewWindow; ?>', 'status=no', 'titlebar=0'); } </script> <form id='testform_eq' name='testform_new'> <input type='text' id='val2' name='val2'> <input type='button' value='Submit' onclick='eq(document.getElementById("val2").value)'> </form> 

Another option is to get the value inside the function, as described here . 另一种选择是让里面的函数值,如所描述这里 This way is good if you're not going to reuse eq with different values. 如果您不打算重复使用具有不同值的eq ,这种方式很好。 You have to choose one of them depending on your real case. 你必须根据你的实际情况选择其中一个。

You need to use value property of the input. 您需要使用输入的value属性。

Update code 更新代码

 <input type='button' value='Submit' 
 onclick='eq(document.getElementById(val2).value)'>

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

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