Javascript 如何从“中获取值”;标签盒“;
我对“标记框”有以下代码: HTML:Javascript 如何从“中获取值”;标签盒“;,javascript,php,jquery,Javascript,Php,Jquery,我对“标记框”有以下代码: HTML: JQuery: $(document).ready(function(){ $('#tags input').on('focusout',function(){ var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,''); if(txt) { $(this).before('<span class="tag">'+ txt.t
JQuery:
$(document).ready(function(){
$('#tags input').on('focusout',function(){
var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,'');
if(txt) {
$(this).before('<span class="tag">'+ txt.toLowerCase() +'</span>');
$(this).before('<input>').attr({
type: 'hidden',
name: 'tags[]',
value: txt.toLowerCase();
}
this.value="";
}).on('keyup',function( e ){
if(/(188|13)/.test(e.which))
$(this).focusout();
});
$('#tags').on('click','.tag',function(){
$(this).remove();
});
});
$(文档).ready(函数(){
$('#tags input')。on('focusout',function(){
var txt=此.value.replace(/[^a-zA-Z0-9\+-\.\\.\\\]/g');
如果(txt){
$(this).before(“”+txt.toLowerCase()+“”);
$(此).before(“”).attr({
键入:“隐藏”,
名称:'tags[]',
值:txt.toLowerCase();
}
此。值=”;
}).on('keyup',功能(e){
如果(/(188 | 13)/.测试(e.which))
$(this.focusout();
});
$('#tags')。在('单击','.tag',函数()上{
$(this.remove();
});
});
我正在使用隐藏输入将标记发送到服务器。单击“提交”按钮后,表单会将我重定向到另一个.php页面。我正在尝试使用var\u dump($\u POST['tags'))在php端进行访问,
但结果是数组(size=1)
0=>字符串“”(长度=0)
有人能帮忙吗?您可以使用隐藏的输入将标签发送到服务器,这不会使任何输入在页面上可见,但会在您提交表单时发送
$(document).ready(function(){
$('#tags input').on('focusout',function(){
var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,'');
if(txt) {
$(this).before('<span class="tag">'+ txt.toLowerCase() +'</span>');
$(this).before('<input>').attr({
type: 'hidden',
name: 'tags[]',
value: txt.toLowerCase()
});
}
this.value="";
}).on('keyup',function( e ){
if(/(188|13)/.test(e.which))
$(this).focusout();
});
$('#tags').on('click','.tag',function(){
$(this).remove();
});
});
$(文档).ready(函数(){
$('#tags input')。on('focusout',function(){
var txt=此.value.replace(/[^a-zA-Z0-9\+-\.\\.\\\]/g');
如果(txt){
$(this).before(“”+txt.toLowerCase()+“”);
$(此).before(“”).attr({
键入:“隐藏”,
名称:'tags[]',
值:txt.toLowerCase()
});
}
此。值=”;
}).on('keyup',功能(e){
如果(/(188 | 13)/.测试(e.which))
$(this.focusout();
});
$('#tags')。在('单击','.tag',函数()上{
$(this.remove();
});
});
然后在php端访问,如
var\u dump($\u POST['tags');
您可以在服务器端进行访问。如果php是您的服务器端语言,那么您可以使用php而不是Javascript进行访问。您的
标记需要具有name
属性,否则该字段中的数据将无法提交
如果form方法是GET,那么您的PHP脚本应该在$\u GET数组中找到该值。如果是POST,那么它将在$\u POST数组中。将其保存到数据库中的实际代码取决于几个因素,包括您是使用普通PHP还是某些MVC框架、您使用的数据库、特定的数据库配置和名称如果您提供更多这些细节,可能有人能够用特定的代码回答。为什么要标记
PHP
?@Hassaan,因为我想用PHP来解决它e.
包含数字而不是字符串,所以使用.test()
对它不起作用。用e.which==13 | | e.which==188
代替。@Mottie实际上它起作用了,我有另一个问题)我刚试过,但当我试着打印$\u POST['tags']时,它是“Array”而不是我的标签。使用var\u dump($\u POST['tags']))
,它可以包含多个标记。在这种情况下,Print仅显示变量的类型。或者您可以使用foreach($tag['tags']作为$tag)Print$tag;
它打印数组(size=1)0=>string'(length=0):(看起来txt.toLowerCase()
可能为空,更改值:txt.toLowerCase()
到value:“测试值”
并查看服务器在这种情况下返回的内容。请使用网页的简化测试用例示例更新答案,看起来问题出在另一个地方。
$(document).ready(function(){
$('#tags input').on('focusout',function(){
var txt= this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g,'');
if(txt) {
$(this).before('<span class="tag">'+ txt.toLowerCase() +'</span>');
$(this).before('<input>').attr({
type: 'hidden',
name: 'tags[]',
value: txt.toLowerCase()
});
}
this.value="";
}).on('keyup',function( e ){
if(/(188|13)/.test(e.which))
$(this).focusout();
});
$('#tags').on('click','.tag',function(){
$(this).remove();
});
});