简体   繁体   English

如何将输入值传递给功能参数?

[英]How do I pass input value to function parameter?

I want to pass input field value to function parameter, reverse string and print it out. 我想将输入字段值传递给函数参数,反转字符串并将其打印出来。

I have figured out how to print input field text but I have not a clue how to pass it through a function. 我已经弄清楚了如何打印输入字段文本,但是我不知道如何通过函数传递它。

<form>
  <p>Input your text here:</p>
  <input type="text" id="inputFieldValue" />
  <input type="button" id="buttonClick" value="Click me!">
</form>

<span id="resultHere"></span>

/* this one works but it just prints input field text*/ / *这是可行的,但它只打印输入字段文本* /

$('#buttonClick').click(function() { 
    print = $('#inputFieldValue').val();
    $('#resultHere').text(print);
}); 

/*I don't know how to make this work: */ / *我不知道如何进行这项工作:* /

someText = $('#inputFieldValue').val();

$('#buttonClick').click(function reverseMe(print) {
    var printMe = print.split('').reverse().join('');
    $('#resultHere').text(printMe);
});
reverseMe(someText);

I just want it to reverse string and print into HTML. 我只希望它反转字符串并打印为HTML。

Example input: 'Hello, World!' 输入示例:“您好,世界!” Output: '!dlroW ,olleH' 输出:'!dlroW,olleH'

I want to use JS or Jquery for this. 我想为此使用JS或Jquery。

Try defining the function reverseMe outside the event handler, and declaring the variable someText inside the handler's function. 尝试在事件处理程序外部定义函数reverseMe ,然后在处理程序的函数内部声明变量someText

This should do it: 应该这样做:

$('#buttonClick').click(function() {
  someText = $('#inputFieldValue').val();
  reverseMe(someText);
});

function reverseMe(print) {
    var printMe = print.split('').reverse().join('');
    $('#resultHere').text(printMe);
}

I suggest to first create a function that reverse a string and returns it, that will be his only purpose. 我建议首先创建一个将string反向并返回的函数,这将是他唯一的目的。 Then use it inside the click event handler before displaying the text , like this: 然后在显示text之前在click事件处理程序中使用它,如下所示:

 $('#buttonClick').click(function() { let print = $('#inputFieldValue').val(); $('#resultHere').text(reverseStr(print)); }); const reverseStr = (inputStr) => inputStr.split("").reverse().join(""); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <p>Input your text here:</p> <input type="text" id="inputFieldValue" /> <input type="button" id="buttonClick" value="Click me!"> </form> <span id="resultHere"></span> 

Why not to include the logic that also shows the text in the span element inside the reverseStr method? 为什么不在reverseStr方法内部包括在span元素中也显示文本的逻辑? Because you will lose reusability of that method. 因为您将失去该方法的可重用性。

Note also, function arrow expression are not fully supported . 另请注意, 不完全支持 功能箭头表达式 So you may consider using a standard function declaration instead. 因此,您可以考虑使用标准函数声明。

function reverseStr(inputStr)
{
    return inputStr.split("").reverse().join("");
}

Extract the method out of the event listener, and call it from the listener, passing in the value to reverse. 从事件侦听器中提取方法,然后从侦听器中调用该方法,并传入值以进行反转。

function reverseMe(print) {
    var printMe = print.split('').reverse().join('');
    $('#resultHere').text(printMe);
}

someText = $('#inputFieldValue').val();

$('#buttonClick').click(function(){
    reverseMe($('#inputFieldValue').val());
});

reverseMe(someText);

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

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