我想通过javascript基于PHP条件隐藏提交按钮

时间:2015-08-13 09:29:58

标签: javascript php

<?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
}
?>

这里我正在评估一个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>

条件评估为true,但提交按钮仍然可见。我如何实现这一目标?

8 个答案:

答案 0 :(得分:6)

尝试使用这种方式......

<input type='submit' <?php if($result5->num_rows>0) {?> disabled="disabled" <?php } ?> id='submitbtn' name='save' value='SAVE' class='form_btn' >

可能会有所帮助

答案 1 :(得分:3)

我绑定了你的代码,这些工作正常,所以我认为必须有其他问题来影响你的js代码,例如submitbtn的同名ID或js错误。

答案 2 :(得分:1)

如果你想使用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
}
?>

你也可以通过使用php来实现这一点。

<input type='submit' id='submitbtn' name='save' value='SAVE' class='form_btn' <?php if(mysql_num_rows($result5)>0) echo 'style="display:none"';?>>

答案 3 :(得分:0)

请尝试以下操作: jQuery必须在您的页面上。

在页面就绪时调用语句将解决问题。

<?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
}
?>

答案 4 :(得分:0)

我认为你在加载form之前添加了你的php代码!

所以你需要window.onload 将你的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
}
?>

答案 5 :(得分:0)

发布答案,因为所有其他人都只是禁用按钮。它仍然可见。所以要隐藏按钮,请使用:

window.onload = function(){
   document.getElementById("<id of your submit button>").style.display = "none";
}

if(condition=true)

OR

<input type='submit' name="sbmt" id="id_1"<?php if(condition=true) { ?> style="display: none" <?php } ?> />

但是因为你的问题特别要求“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 } ?>
     />

答案 6 :(得分:0)

<?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>';
}
?>

答案 7 :(得分:-2)

它不是ImageView mImageView; PhotoViewAttacher mAttacher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Any implementation of ImageView can be used! mImageView = (ImageView) findViewById(R.id.iv_photo); // Set the Drawable displayed Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper); mImageView.setImageDrawable(bitmap); // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality. mAttacher = new PhotoViewAttacher(mImageView); } // If you later call ImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call attacher.update();

none
相关问题