单击Submit按钮将其值更改为其他值


Submit button clicking changing the value of it to another thing

也许很容易!我是php程序员,我没有js的经验,但我必须为我的一个代码做这件事假设我在点击后的页面中有sub1,它一定是sub1,但现在的值是sub2

       <html>
        <head>
        <title>pharmacy</title>
        </head>
        <body>
        <form method="post" action="pharmacy.php">
        <?php
       //some code
            if(array_key_exists('update',$_POST)){
                //somecode
                }
        ?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
    </form>
    </body>
    </html>

show作为函数名在这里没有意义(imo),但您可以这样做:

<input type="submit" name="sub" value="sub1" onclick="show(this)">

function show(element) {
    element.value = 'sub2';
}

重要信息:

但这实际上并不能解决你的问题。单击按钮后,表单即被提交,这意味着浏览器会启动一个新请求,并加载一个新页面。所以你对当前页面所做的每一次更改都会丢失。

问题是:你想做什么?

在我看来,您应该更改服务器端按钮的值。你必须跟踪提交的表单(或者多久提交一次,我不知道你想做什么),并相应地设置按钮的值。

更新:

我看到了解决这个问题的几种可能性:

  1. 您可以继续使用JavaScript,并通过Ajax发送和获取数据。由于您没有使用JavaScript的经验,我想说,在使用它之前,您必须先了解更多关于JavaScript和Ajax的信息

  2. 您可以在URL中添加一个GET参数,通过该参数可以知道要为按钮显示哪个标签。示例:

    <form method="post" action="?update=1">
    

    <input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
    
  3. 类似于2,但使用会话变量(而不是GET参数)来跟踪状态。

更新2:

由于您已经拥有$_POST['update'],因此不需要URL参数。可能只是:

<html>
    <head>
        <title>pharmacy</title>
    </head>
    <body>
        <form method="post" action="pharmacy.php">
        <input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
        </form>
    </body>
</html>

这应该是

        function show(){
        document.getElementsByName('sub')[0].value = 'sub2';
        return false;
        }

编辑:如果您不希望它提交表单,只需添加一个return false,但您需要将onclick从提交按钮更改为表单onsubmit

<html>
<head>
<title>test</title>
<script>
function show()
{
    document.getElementById("sub").value= "sub2";
            return true;
}
</script>
</head>
<body>
<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>