简体   繁体   English

如何使用提交按钮创建一个文本框,使用js或jQuery单击javascript函数执行回调?

[英]How do I create a text box with a submit button that performs a callback on click to a javascript function using js or jQuery?

I'm new to javascript and am having a hard time finding the answer to this. 我是javascript的新手,很难找到答案。 Any help is appreciated. 任何帮助表示赞赏。

If you had the following HTML: 如果您有以下HTML:

<input type="text" />
<input id="button" type="button" value="Click Me" />

You can bind a function to be executed when a click event fires on that button like this: 您可以绑定要在单击事件触发该按钮时执行的函数,如下所示:

$(function() {
    $('#button').click(function() {
        // Do logic here
    });
});

Creating it all on the fly 动态创建它

You can insert the previous HTML into your document using jQuery. 您可以使用jQuery将以前的HTML插入到文档中。 First select another element in the DOM which locates where you wish to insert the HTML: 首先在DOM中选择另一个元素,该元素定位您希望插入HTML的位置:

$('#someElement')

Then you can use any of the jQuery insertion methods in order to inject some HTML of your own: 然后你可以使用任何jQuery插入方法来注入你自己的一些HTML:

$('#someElement')
    .append('<input type="text" />')
    .append('<input id="button" type="button" value="Click Me" />');

Now that your elements are in the DOM, you can select them and bind a callback to the click event in the normal way: 既然你的元素在DOM中,你可以选择它们并以正常方式将回调绑定到click事件:

$('#button').click(function() {
    // Do logic here
});

the html : <textarea id="txt"></textarea><input type="button" id="button">submit</input> html: <textarea id="txt"></textarea><input type="button" id="button">submit</input>
and the js: 和js:


var d = document.getElementById('button');// identifying the button 
function myFunction(){
   var text = document.getElementById('txt').value;//getting the user's input
   // and whatever you want to do with the user's input
}
d.addEventListener('click',myFunction,false);//attaching myFunction to be called on the button click`
If you use jQuery, the syntax is a bit simpler : 如果你使用jQuery,语法有点简单:
 $('#button').click(myFunction); // or $('#button').click(function(){ var text = $('#txt').val(); //and again your logic here }); 

暂无
暂无

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

相关问题 在HTML中,我需要创建一个执行onclick事件的提交按钮,这是一个使用距离公式来计算距离的JS函数 - In HTML I need to create a submit button that performs an onclick event, a JS function to calculate distance using the distance formula 如何创建带有函数的按钮,然后使用jQuery提交按钮? - How do I create a button with a function, then submit it, with jQuery? 单击“提交”按钮时如何调用Javascript函数? - How do I call a Javascript function when I click a “Submit” button? 如何在Javascript中向.submit()添加回调函数? - How do I add a callback function to .submit() in Javascript? 单击后如何使用JavaScript或jQuery更改Submit按钮的值? - How to change the value of submit button after click using JavaScript or jQuery? 如何在 JavaScript 中创建一个执行 Sigma 符号计算的函数? - How do you create a function that performs Sigma notation calculations in JavaScript? 当我使用jQuery单击选择按钮时,如何选择所有复选框 - How do I select all check box when I click on Select button using jQuery 如何在没有提交按钮的情况下从文本字段调用javascript函数? - How do i call a javascript function from a text field without submit button? 我如何创建一个 javascript function 在按下表单提交按钮时更改 html - how do I create a javascript function that changes the html when a form submit button is pushed in django 如何使用javascript在点击下拉列表上创建文本框? - How to create a text box on click dropdown using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM