[英]In HTML I need to create a submit button that performs an onclick event, a JS function to calculate distance using the distance formula
[英]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?
我是javascript的新手,很難找到答案。 任何幫助表示贊賞。
如果您有以下HTML:
<input type="text" />
<input id="button" type="button" value="Click Me" />
您可以綁定要在單擊事件觸發該按鈕時執行的函數,如下所示:
$(function() {
$('#button').click(function() {
// Do logic here
});
});
您可以使用jQuery將以前的HTML插入到文檔中。 首先在DOM中選擇另一個元素,該元素定位您希望插入HTML的位置:
$('#someElement')
然后你可以使用任何jQuery插入方法來注入你自己的一些HTML:
$('#someElement')
.append('<input type="text" />')
.append('<input id="button" type="button" value="Click Me" />');
既然你的元素在DOM中,你可以選擇它們並以正常方式將回調綁定到click事件:
$('#button').click(function() {
// Do logic here
});
html: <textarea id="txt"></textarea><input type="button" id="button">submit</input>
和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`
如果你使用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.