我正在做一个网站设置页面(我想),网站的用户可以编辑在网站公共页面上看到的页面值。(这只限于登录用户)
我有一个表单(如下图所示),当点击时,执行AJAX并 "发布 "更新内容页面。
我想我的问题是,我如何才能改变下面的代码,使它改变基于按钮的文本体。
更简单的是,我怎样才能使整个页面的点击事件适用于所有按钮,我可以知道哪个按钮被按下了?
我知道下面只有一个文本字段和一个按钮,但将来会有更多的按钮,因此我需要这个功能。
我的 e.js
文件。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/admin.css">
<title>Manage the website</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#submitButton").click(function(){
$.ajax({
url: 'update',
type: 'POST',
data: {
toUpdate: 'homepage',
text: document.getElementById('textField').value // Select a text field based on the button
},
});
});
});
</script>
</head>
<header>
<!-- Put partial includes here -->
</header>
<div class="topPage">
<h1>Hello, <%= firstName %>!</h1>
<p1>
Find a value you would like to edit, then press send. The website can take up to 10 mins for the changes to apply. The changes will not change live.
</p1>
</div>
<div class="homepage">
<div class="information">
<h1>
Homepage variables
</h1>
<p1>
Variables for the 'homepage' page are displayed below. Changes can take up to 10 mins to apply globally.
</p1>
<hr>
</div>
<div class="form">
<label>
Change the 'body' of 'homepage'
</label>
<form action="nothing" method="post">
<input type="text" id="textField" name="text" value="<%= pageData.get('homepage').homeBody%>" required>
<button type="button" class="submit" id="submitButton">Submit Changes</button>
</form>
</div>
</div>
<script>
</script>
</html>
先谢谢了!
你可以这样做。
$("button").on("click", function() {
console.log($(this).attr("id"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="this">
This
</button>
<button id="that">
That
</button>
选择表单中的输入标签,并跟踪更改事件。
$("form :input").on("change", function(){
var $elm = $(this); // the button which is clicked
// do your ajax here
});