javascript 我想通过javascript隐藏基于PHPcondition的提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31984440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to hide the submit button based on a PHPcondition through javascript
提问by iaaphpd13
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
document.getElementById("submitbtn").style.display = "none";
</script>
<?php
}
?>
here I am evaluating an if clause, if the condition is true, I want to hide the submit button of the form.
在这里我正在评估一个 if 子句,如果条件为真,我想隐藏表单的提交按钮。
<form action='xyz.php'>
<input type='text'>
<input type='text'>
<input type='text'>
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' >
</form>
The condition is evaluating to true, but the submit button is still visible. How do I achieve this??
条件评估为真,但提交按钮仍然可见。我如何实现这一目标?
回答by Sahil Manchal
try using this way....
尝试使用这种方式....
<input type='submit' <?php if($result5->num_rows>0) {?> disabled="disabled" <?php } ?> id='submitbtn' name='save' value='SAVE' class='form_btn' >
may be it will help
可能会有所帮助
回答by gangzi
I had tied your code,and these worked ok, so I think must have other problems to affected your js code, such as a same name Id of submitbtn
, or a js error.
我已经绑定了你的代码,这些工作正常,所以我认为肯定有其他问题会影响你的 js 代码,例如同名 Idsubmitbtn
或 js 错误。
回答by Ishan Srivastava
Posting the answer because all others are just disabling the button. It will still be visible. So to hide the button, use:
发布答案是因为所有其他人都只是禁用按钮。它仍然是可见的。所以要隐藏按钮,请使用:
window.onload = function(){
document.getElementById("<id of your submit button>").style.display = "none";
}
inside the if(condition=true)
在 - 的里面 if(condition=true)
OR
或者
<input type='submit' name="sbmt" id="id_1"<?php if(condition=true) { ?> style="display: none" <?php } ?> />
but since your question has asked exceptionally for "javascript" i will go one step further and write
但由于您的问题特别要求“javascript”,我将更进一步写
<input type='submit' name="sbmt" id="id_1"
<?php if(condition=true) { ?>
<script type='text.javascript'>
document.getElementById("id_1").style.display = "none";
</script>
<?php } ?>
/>
回答by Happy Coding
If you want to use js try this :
如果你想使用 js 试试这个:
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$(document).ready(function(){
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
You can achieve this by using php too.
您也可以通过使用 php 来实现这一点。
<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' <?php if(mysql_num_rows($result5)>0) echo 'style="display:none"';?>>
回答by AnkiiG
Try as below : jQuery must be there on your page.
尝试如下:jQuery 必须在您的页面上。
On calling the statement on page ready will solve the problem.
调用页面准备好的语句将解决问题。
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
// the above echo is just for testing
?>
<script type='text/javascript'>
$( document ).ready(function() {
document.getElementById("submitbtn").style.display = "none";
});
</script>
<?php
}
?>
回答by Loukoulele
<?php
if(mysql_num_rows($result5)>0)
{
echo'<div style='visibility:hidden' id="del">';
echo'<script>
$(document).ready(function(e) {
$("#del").css("visibility", "visible");
});
</script>';
}
?>
回答by Nabi
I Think you added your php codes before that form
loads!
我认为您在form
加载之前添加了 php 代码!
So you need window.onload
ORReplace your php codes to after form
所以你需要window.onload
OR替换你的 php 代码到之后form
<?php
if(mysql_num_rows($result5)>0)
{
echo 'condition matched';
?>
<script type='text/javascript'>
window.onload = function(){
document.getElementById("submitbtn").style.display = "none";
}
</script>
<?php
}
?>
回答by Hearner
It is not none
it's :
不是none
它是:
<script type='text/javascript'>
document.getElementById("submitbtn").style.visibility = "hidden";
</script>