Javascript 检查是否单击了按钮。。。不起作用
仅对我的代码的一部分做一个非常简短的解释:Javascript 检查是否单击了按钮。。。不起作用,javascript,php,html,button,isset,Javascript,Php,Html,Button,Isset,仅对我的代码的一部分做一个非常简短的解释: 我有两个按钮可以做不同的事情 其中之一允许用户在表/数据库中搜索他/她想要搜索的任何内容 另一个允许用户将内容插入数据库 我想做什么: 我试图检查用户单击了哪个按钮,以便执行相应的代码 我一直在四处寻找,几乎无论我走到哪里,人们都建议使用isset(),但它对我不起作用。也许我不完全理解isset()的作用,但它基本上不是检查变量是否被设置了吗 这是我的密码: 功能显示(x,y){ } 执行搜索 插入数据 您没有将按钮sButton或iButt
- 我有两个按钮可以做不同的事情李>
- 其中之一允许用户在表/数据库中搜索他/她想要搜索的任何内容
- 另一个允许用户将内容插入数据库
功能显示(x,y){
}
执行搜索
插入数据
您没有将按钮sButton
或iButton
传递到test2.php,因为您的第二个和第三个表单没有将它们作为输入。仅提交每个特定表格内的输入。有按钮的表单没有动作,只有按钮调用JS函数
我建议您为提交到test2.php的每个表单添加隐藏字段,如下所示:
<form id = "searchForm" value "search" style = "display: none;" action = "test2.php" method = "post">
<input type="hidden" name = "sButton" value="sButton" />
<!-- Do something -->
</form>
<form id = "insertForm" style = "display: none;" action = "test2.php" method = "post">
<input type="hidden" name = "iButton" value="iButton" />
<!-- Do something -->
</form>
这样您的test2.php应该可以工作。您没有将按钮
sButton
或iButton
传递到test2.php,因为您的第二个和第三个表单没有它们作为输入。仅提交每个特定表格内的输入。有按钮的表单没有动作,只有按钮调用JS函数
我建议您为提交到test2.php的每个表单添加隐藏字段,如下所示:
<form id = "searchForm" value "search" style = "display: none;" action = "test2.php" method = "post">
<input type="hidden" name = "sButton" value="sButton" />
<!-- Do something -->
</form>
<form id = "insertForm" style = "display: none;" action = "test2.php" method = "post">
<input type="hidden" name = "iButton" value="iButton" />
<!-- Do something -->
</form>
这样,test2.php就可以工作了。添加一个
进入搜索表单,然后
插入到插入表单中
在那之后。您需要在show(…)javascript函数中提交(所选)表单
进入搜索表单,然后
插入到插入表单中
在那之后。您需要在show(…)javascript函数中提交(所选)表单使用:
例如:
<button name = "sButton" type = "button" onclick = 'show(this.name, "searchForm", "insertForm");'>Perform Search</button>
function show(name, x, y){
alert(name);
if(name === "sButton"){
do this....
}
}
演示 使用:
例如:
<button name = "sButton" type = "button" onclick = 'show(this.name, "searchForm", "insertForm");'>Perform Search</button>
function show(name, x, y){
alert(name);
if(name === "sButton"){
do this....
}
}
演示
我不确定您想做什么,但我不明白为什么按钮会出现在表单中。考虑动态地连接侦听器,向按钮添加一个名称或ID,这样你就可以知道点击哪个按钮,然后根据点击的方式隐藏或显示表单:
// The buttons don't seem to need to be in a form, so use some other
// container so you don't need to worry about a useless form being
// submitted
<div id="buttonContainer">
<button id="searchButton">Perform search</button>
<button id="insertButton">Insert data</button>
</div>
// Forms for testing
<form id="searchForm"><input value="search"></form>
<form id="insertForm"><input value="insert"></form>
//按钮似乎不需要以某种形式出现,因此请使用其他形式
//容器,这样您就不必担心一个无用的表单
//提交
执行搜索
插入数据
//测试表格
以及守则:
<script>
// Hide and show forms depending on which button was clicked using the
// button's ID
function showForm(event) {
// If the search button was clicked, show the search form and hide the
// input form
if (/search/.test(this.id)) {
document.getElementById('searchForm').style.display = '';
document.getElementById('insertForm').style.display = 'none';
// If the insert button was clicked, do the opposite
} else {
document.getElementById('searchForm').style.display = 'none';
document.getElementById('insertForm').style.display = '';
}
}
// Attach listeners to the buttons
window.onload = function() {
Array.prototype.forEach.call(document.querySelectorAll('#searchButton, #insertButton'),
function(button) {
button.addEventListener('click', showForm, false);
}
);
// Hide the forms
Array.prototype.forEach.call(document.querySelectorAll('#searchForm, #insertForm'),
function(form) {
form.style.display = 'none';
}
);
}
</script>
//隐藏和显示表单,具体取决于使用
//按钮ID
功能展示形式(活动){
//如果单击了搜索按钮,则显示搜索表单并隐藏
//输入表格
if(/search/.test(this.id)){
document.getElementById('searchForm').style.display='';
document.getElementById('insertForm').style.display='none';
//如果单击了“插入”按钮,则执行相反的操作
}否则{
document.getElementById('searchForm').style.display='none';
document.getElementById('insertForm')。style.display='';
}
}
//将侦听器附加到按钮
window.onload=函数(){
Array.prototype.forEach.call(document.queryselectoral(“#searchButton,#insertButton”),
功能(按钮){
按钮。addEventListener('click',showForm,false);
}
);
//隐藏表格
Array.prototype.forEach.call(document.queryselectoral(“#searchForm,#insertForm”),
功能(形式){
form.style.display='none';
}
);
}
我不确定您想做什么,但我不明白为什么按钮会出现在表单中。考虑动态地连接侦听器,向按钮添加一个名称或ID,这样你就可以知道点击哪个按钮,然后根据点击的方式隐藏或显示表单:
// The buttons don't seem to need to be in a form, so use some other
// container so you don't need to worry about a useless form being
// submitted
<div id="buttonContainer">
<button id="searchButton">Perform search</button>
<button id="insertButton">Insert data</button>
</div>
// Forms for testing
<form id="searchForm"><input value="search"></form>
<form id="insertForm"><input value="insert"></form>
//按钮似乎不需要以某种形式出现,因此请使用其他形式
//容器,这样您就不必担心一个无用的表单
//提交
执行搜索
插入数据
//测试表格
以及守则:
<script>
// Hide and show forms depending on which button was clicked using the
// button's ID
function showForm(event) {
// If the search button was clicked, show the search form and hide the
// input form
if (/search/.test(this.id)) {
document.getElementById('searchForm').style.display = '';
document.getElementById('insertForm').style.display = 'none';
// If the insert button was clicked, do the opposite
} else {
document.getElementById('searchForm').style.display = 'none';
document.getElementById('insertForm').style.display = '';
}
}
// Attach listeners to the buttons
window.onload = function() {
Array.prototype.forEach.call(document.querySelectorAll('#searchButton, #insertButton'),
function(button) {
button.addEventListener('click', showForm, false);
}
);
// Hide the forms
Array.prototype.forEach.call(document.querySelectorAll('#searchForm, #insertForm'),
function(form) {
form.style.display = 'none';
}
);
}
</script>
//隐藏和显示表单,具体取决于使用
//按钮ID
功能展示形式(活动){
//如果单击了搜索按钮,则显示搜索表单并隐藏
//输入表格
if(/search/.test(this.id)){
document.getElementById('searchForm').style.display='';
document.getElementById('insertForm').style.display='none';
//如果单击了“插入”按钮,则执行相反的操作
}否则{
document.getElementById('searchForm').style.display='none';
document.getElementById('insertForm')。style.display='';
}
}
//将侦听器附加到按钮
window.onload=函数(){
Array.prototype.forEach.call(document.queryselectoral(“#searchButton,#insertButton”),
功能(按钮){
按钮。addEventListener('click',showForm,false);
}
);
//隐藏表格
Array.prototype.forEach.call(document.queryselectoral(“#searchForm,#insertForm”),
功能(形式){
form.style.display='none';
}
);
}
您现在是否能够通过检查显示功能中的x
和y
的值来确定单击了哪个按钮?如果您为按钮命名,则单击的按钮将与表单一起提交。或者,监听器可以使用show(“searchForm”、“insertForm”、this.name)
将按钮的名称传递给函数。为什么按钮是一种形式?@sfletche嗯,我想是的?我的按钮现在按我想要的方式工作,所以我觉得没问题。你现在能阻止吗