简体   繁体   English

如何将输入框值发送到javascript函数参数

[英]How to send the inputbox value to a javascript function parameter

Given this html... 鉴于此html ...

<input id="txtbox-page" maxlength="4" />

how can I create a button or a link that executes a javascript function, where its parameter is the input box value? 如何创建一个按钮或一个执行javascript函数的链接,其参数为输入框的值?

I have to call a function, that is already in production, that has this format: 我必须调用一个已经在生产中的具有以下格式的函数:

javascript:pageClient('?p=2');

where the number 2, has to be the value of the input box. 其中数字2必须是输入框的值。

HTML: HTML:

<a id="mylink" href="foo.bar">Link</a>

JS: JS:

var box = document.getElementById('txtbox-page');
document.getElementById('mylink').addEventListener('click', function(){
    // Use `box` here
    pageClient('?p=' + +box.value);
}, false);

The second + is optional. 第二个+是可选的。 I used it to convert to number in order to avoid injection attacks. 我用它来转换为数字以避免注入攻击。

You could do something along these lines: 您可以按照以下方式进行操作:

function GetTxtBoxValue() {
    var txtBoxPage = document.getElementByID("txtbox-page");
    pageClient('?p='+txtBoxPage.value);
}

Then bind that function to your form submission. 然后将该功能绑定到您的表单提交。

HTML 的HTML

<button id="pageClientSubmitter">Click me</button>

JS JS

var pageClientSubmitter = document.getElementById('pageClientSubmitter');

pageClientSubmitter.onclick = function () {
  var val = document.getElementById('txtbox-page').value;
  pageClient('?p=' + val);
}

Demo 演示版

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

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