javascript 单击提交按钮将其值更改为其他内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6226246/
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
Submit button clicking changing the value of it to another thing
提问by Nickool
maybe very easy! I'm php coder and I don't have experience in js but I must do this for one of my codes suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2
也许很容易!我是 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>
回答by Felix Kling
show
as function name does not really make sense here (imo), but you could do:
show
因为函数名称在这里没有意义(imo),但你可以这样做:
<input type="submit" name="sub" value="sub1" onclick="show(this)">
and
和
function show(element) {
element.value = 'sub2';
}
Important:
重要的:
But that will actually not solve your problem. As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page. So every change you made the current page is lost anyway.
但这实际上并不能解决您的问题。一旦您单击按钮,表单就会被提交,这意味着浏览器会发起一个新请求并将加载一个新页面。因此,无论如何,您对当前页面所做的每项更改都会丢失。
The question is: What are you trying to do?
问题是:你想做什么?
It seems to me that you should change the value of the button on the server side. You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.
在我看来,您应该更改服务器端按钮的值。您必须跟踪提交了哪个表单(或多久提交一次,我不知道您要做什么)并相应地设置按钮的值。
Update:
更新:
I see several possibilities to solve this:
我看到了解决这个问题的几种可能性:
You could keep using JavaScript and send and get the data via Ajax. As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.
You could add a GET parameter in your URL with which you can know which label to show for the button. Example:
<form method="post" action="?update=1">
and
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
Similar to 2, but use a session variable(and not a GET parameter) to keep track of the state.
您可以继续使用 JavaScript 并通过Ajax发送和获取数据。由于您没有使用 JavaScript 的经验,我想说您必须先了解有关 JavaScript 和 Ajax 的更多信息,然后才能使用它。
您可以在您的 URL 中添加一个 GET 参数,通过它您可以知道要为按钮显示哪个标签。例子:
<form method="post" action="?update=1">
和
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
类似于 2,但使用会话变量(而不是 GET 参数)来跟踪状态。
Update2:
更新2:
As you are already having $_POST['update']
you don't need the URL parameter. It could just be:
正如您已经拥有的$_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>
回答by Pradeep
<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>
回答by Niklas
This should do it
这应该做
function show(){
document.getElementsByName('sub')[0].value = 'sub2';
return false;
}
Edit:if you don't want it to submit the form, just add a return false
, but then you'd need to change your onclick
from your submit button to your forms onsubmit
;
编辑:如果您不希望它提交表单,只需添加一个return false
,但是您需要将onclick
您的提交按钮更改为您的表单onsubmit
;