Javascript onchange 重载页面,值为 select
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12330903/
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
onchange reload page with value of select
提问by user1547410
I have a table displaying results, I want to give people the ability to update the results of a cell/field individually without needing a whole update form
我有一个显示结果的表格,我想让人们能够在不需要整个更新表单的情况下单独更新单元格/字段的结果
Here is what I have:
这是我所拥有的:
echo "<select name='tv_taken' id='tv_taken' onchange='window.location=\"samepage.php?update=tv_taken&update_id=$tv_id\" '>";
As you can probably tell its a select box with a list of users. Now the only problem is i need to pass the new selected value in the url string, so i want the new value of the select.
正如您可能会告诉它一个带有用户列表的选择框。现在唯一的问题是我需要在 url 字符串中传递新的选定值,所以我想要选择的新值。
So lets say it was mary and someone chooses a new user John I want something like this
所以让我们说是玛丽,有人选择了一个新用户约翰,我想要这样的东西
echo "<select name='tv_taken' id='tv_taken' onchange='window.location=\"samepage.php?update=tv_taken&update_id=$tv_id&user_name=$find_username\" '>";
What I'm not sure on is there a way to pass the changed value via php or will I need a javascript solution?
我不确定有没有办法通过 php 传递更改后的值,或者我需要一个 javascript 解决方案吗?
Ill then use an
生病然后使用
IF if(isset($_GET['tv_id']))
{
Do the update
}
This is not a public site so I'm guessing there is a more secure way to do this but this should probably suffice since it will only be team members using it who would be required to login before they can see this page.
这不是一个公共站点,所以我猜有一种更安全的方法可以做到这一点,但这可能就足够了,因为只有使用它的团队成员才需要登录才能看到此页面。
回答by Nathan Taylor
You'll want to use JavaScript to achieve this.
您将需要使用 JavaScript 来实现这一点。
Here's a jQuery solution:
这是一个jQuery解决方案:
$('#tv_taken').change(function() {
window.location = "samepage.php?update=tv_taken&update_id=" + $(this).val();
});
And here's a vanilla JavaScript solution:
这是一个普通的 JavaScript 解决方案:
document.getElementById('tv_taken').onchange = function() {
window.location = "samepage.php?update=tv_taken&update_id=" + this.value;
};
回答by xandercoded
window.addEventListener('load', function(){
var select = document.getElementById('tv_taken');
select.addEventListener('change', function(){
window.location = 'pagename.php?tv_id=' + this.value;
}, false);
}, false);