我试图让一个 sql 查询在下拉菜单选择上运行。这是我目前所拥有的:
<script>
$('#templateid').change(function(DoUpdateOnSelect) {
$.post('page.edit.php?page_id=(-->Need help in getting this id from the page url<--)&template='+this.options[this.selectedIndex].value);
});
</script>
下拉菜单:
<select name="templateid" id="templateid" onchange="DoUpdateOnSelect">
<option >Contact</option>
<option >News</option>
<option >Home</option>
</select>
page.edit.php 有条件,如果检测到页面 ID 和 URL 中的模板,它将运行 sql 查询。从选择下拉菜单中使用 jquery 获取模板参数值(如果该部分在 jquery 中设置错误,请告诉我)。我想不通的是如何从页面的当前 url 中获取 page_id 参数,并将其插入到函数中,以便 page.edit.php?page_id=1&template=Home 成为正确输出的 URL。
如果您需要更多信息,请告诉我。提前致谢。
编辑:基于以下答案:
function querystring(name)
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
$('#templateid').change(function() {
var page_id = querystring('page_id');
var url = '?VIEW=EDITPAGE&linklabel='+page_id+'&template='+(this.options[this.selectedIndex].value);
$.post(url);
});
当我在下拉菜单中进行选择时没有发生任何变化
最佳答案
要从 url 中获取 page_id,你应该使用像
这样的函数function querystring( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
然后你可以用
检索它var page_id = querystring('page_id');
所以,您的 onchange 处理程序看起来像这样:
$('#templateid').change(function() {
page_id = querystring('page_id');
var url = 'page.edit.php?page_id='+page_id+'&template='+this.options[this.selectedIndex].value);
$.post(url, function(response){
//your logic here
});
});
哦,请从选择标记中删除 onchange 属性:
<select name="templateid" id="templateid">
<option >Contact</option>
<option >News</option>
<option >Home</option>
</select>
关于php - 更改下拉菜单执行 jquery 函数和 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9304278/