Javascript PHP:在选择更改时,将表单发布给自己
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3976222/
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
PHP: on select change, post form to self
提问by iamjonesy
It's basically what the title says.. I have a form with a select control that I want to force the form to post back to self on change.
这基本上是标题所说的.. 我有一个带有选择控件的表单,我想强制表单在更改时回发给自己。
$bmsclientlist = $clientobj->getBMSClientList();
echo '<form name="changebmsid" method="post" action="' . $_SERVER['PHP_SELF'] . '"><select name="bmsid">';
foreach($bmsclientlist as $bmsclient) {
$var = '';
if($client['bmsid'] == $bmsclient['id']) {
$var = ' selected="selected"';
}
echo '<option value="' . $bmsclient['id'] .'"'. $var .'>' .$bmsclient['clientname'] . '</option>';
}
echo '</select></form>';
$backupobj = new AdminBackup();
if(isset($_POST['bmsid']){
$statusarray = $backupobj->getStatusTotalsbyId($_POST['bmsid']);
}else{
$statusarray = $backupobj->getStatusTotals();
}
I know it's going to involve some javascript but I'm not too sure how to achieve this.
我知道这将涉及一些 javascript,但我不太确定如何实现这一点。
Any help most appreciated!
任何帮助最感谢!
Thanks,
谢谢,
Jonesy
琼斯
回答by acm
This is a <select>
that will submit the parent form
这是一个<select>
将提交父表单的
<form method="post" action="#" name="myform">
<select name="x" onchange="myform.submit();">
<option value="y">y</option>
<option value="z">z</option>
</select>
</form>
All you have to do is give a name to your <form>
and add the onchange event to your <select>
...
您所要做的就是为您的名称命名<form>
并将 onchange 事件添加到您的<select>
...
Adam is right. While the example above works perfectly, I would do it like this:
亚当是对的。虽然上面的例子工作得很好,但我会这样做:
Using jQuerybut there are many other options available...
使用jQuery,但还有许多其他选项可用...
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
$('#mySelect').change(function(){
myform.submit();
});
});
</script>
</head>
and the form
和表格
<body>
<form method="post" action="" name="myform">
<select name="x" id="mySelect">
<option value="y">y</option>
<option value="z">z</option>
</select>
</form>
</body>
回答by PHPology
a quick fix is this:
快速修复是这样的:
Add an onchange attribute to your selectlist
将 onchange 属性添加到您的选择列表
<select name="bmsid" onchange="javascript: form.submit();">
回答by mellowsoon
Change your select menu to this:
将您的选择菜单更改为:
<select name="bmsid" onchange="document.forms['changebmsid'].submit()">
Update
更新
Here's another possible approach:
这是另一种可能的方法:
<script type="text/javascript">
window.onload = function() {
document.forms['myform'].addEventListener('change', function() {
this.submit();
}, true);
};
</script>
Put that anywhere on your page, and you can leave you form as it is.
把它放在你页面上的任何地方,你就可以保持原样。