首页 > 解决方案 > How to pass user input to jquery .click using eventdata?

问题描述

Have two buttons which use jQuery's .click to activate a function. The function is passed an integer parameter so currently one button passes 3 and one passes 4 to the function sort(). Ideally I would like the user to input whatever integer they like and then have only one button that uses the .click. The user input could be a drop down selector or manually input. This sounds like a form to me but I don't know how to bring a form and .click together. I'm still a beginner on this, so maybe this is a fairly simple GET form that can work with the .click?

My current code includes:

<button class="btn btn-warning" type="submit" id="freeze3">&raquo; Random Groups 3</button>
<button class="btn btn-warning" type="submit" id="freeze4">&raquo; Random Groups 4</button>

$( "#freeze3" ).click(function() {
    if (moving == true) {
        clearInterval(interval);
        setTimeout(sort(3), 1000);
    }
    moving = false;
});

$( "#freeze4" ).click(function() {
    if (moving == true) {
        clearInterval(interval);
        setTimeout(sort(4), 1000);
    }
    moving = false;
});

标签: jqueryforms

解决方案


您可以使用基本的常规文本输入、选择或数字输入。使用纯文本输入,您可以执行以下操作,this.value用于获取用户输入的输入:

$( "#myInput" ).change(function() {
    if (moving == true) {
        clearInterval(interval);
        setTimeout(sort( this.value ), 1000);
    }
    moving = false;
});

当然,您还需要进行一些基本验证,以确保他们输入的内容有效。另请注意,我正在使用更改事件,但您也可以使用模糊。或者你可以有一个文本输入和一个按钮,使用按钮在点击时触发功能。


推荐阅读