Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery将字符串转换为链接_Javascript_Jquery_String_Href - Fatal编程技术网

Javascript JQuery将字符串转换为链接

Javascript JQuery将字符串转换为链接,javascript,jquery,string,href,Javascript,Jquery,String,Href,我有3个选择字段,这些字段的组合值将用作url的额外部分 以下是HTML代码: <select name="cos" id="cos" size="5"> <option value="/squad">Squad</option> <option value="/class">Class</option> </select> <select name="color" id="color" size="5"&g

我有3个选择字段,这些字段的组合值将用作url的额外部分

以下是HTML代码:

<select name="cos" id="cos" size="5">
  <option value="/squad">Squad</option>
  <option value="/class">Class</option>
 </select>
<select name="color" id="color" size="5">
  <option value="/purpleblack">PurpleBlack</option>
  <option value="/redblack">RedBlack</option>
  <option value="/aquablack">AquaBlack</option>
</select>
<select name="year" id="year" size="5">
  <option value="/1984">1984</option>
  <option value="/1985">1985</option>
  <option value="/1986">1986</option>
</select>
<br/><br/>
<div id="output"></div>

现在它正在工作。。在所有3个
selectfields
中选择一个选项后,我得到一个类似
/squad/redblack/1985
的输出。 我想在url中使用此输出,因此它看起来像:

mysite.com/squad/redblack/1985


有没有一个简单的方法可以做到这一点?而且。。要仅在所有三个
selectfields
都选择了一个选项后才能看到链接,只需使用
if
语句,仅在选择了三个选项时生成并显示链接即可。要使其成为URL,只需将获得的路径附加到基本URL即可。比如:

$("select").change(function () { 
  var str = "";
  if(#("select option:selected").length === 3){
    $("select option:selected").each(function () {
      var id = $(this).parent().attr('name');
      str += $(this).attr('value');             
    });
    $("div#output").text("mysite.com" + str);
    // Or, if you want a clickable link and not just a URL:
    // $("div#output").append($("<a>").attr({href: "mysite.com" + str}).append("Click me"));
  }
})
.trigger('change');
$(“选择”).change(函数(){
var str=“”;
如果(#(“选择选项:选定”)。长度==3){
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
});
$(“div#output”).text(“mysite.com”+str);
//或者,如果您想要一个可点击的链接,而不仅仅是一个URL:
//$(“div#output”).append($(“”).attr({href:“mysite.com”+str}).append(“单击我”);
}
})
.触发(“变更”);

要仅在选择了所有三个选项后获取链接,只需使用
if
语句,仅在选择了3个选项时生成并显示链接。要使其成为URL,只需将获得的路径附加到基本URL即可。比如:

$("select").change(function () { 
  var str = "";
  if(#("select option:selected").length === 3){
    $("select option:selected").each(function () {
      var id = $(this).parent().attr('name');
      str += $(this).attr('value');             
    });
    $("div#output").text("mysite.com" + str);
    // Or, if you want a clickable link and not just a URL:
    // $("div#output").append($("<a>").attr({href: "mysite.com" + str}).append("Click me"));
  }
})
.trigger('change');
$(“选择”).change(函数(){
var str=“”;
如果(#(“选择选项:选定”)。长度==3){
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
});
$(“div#output”).text(“mysite.com”+str);
//或者,如果您想要一个可点击的链接,而不仅仅是一个URL:
//$(“div#output”).append($(“”).attr({href:“mysite.com”+str}).append(“单击我”);
}
})
.触发(“变更”);

要仅在选择了所有三个选项后获取链接,只需使用
if
语句,仅在选择了3个选项时生成并显示链接。要使其成为URL,只需将获得的路径附加到基本URL即可。比如:

$("select").change(function () { 
  var str = "";
  if(#("select option:selected").length === 3){
    $("select option:selected").each(function () {
      var id = $(this).parent().attr('name');
      str += $(this).attr('value');             
    });
    $("div#output").text("mysite.com" + str);
    // Or, if you want a clickable link and not just a URL:
    // $("div#output").append($("<a>").attr({href: "mysite.com" + str}).append("Click me"));
  }
})
.trigger('change');
$(“选择”).change(函数(){
var str=“”;
如果(#(“选择选项:选定”)。长度==3){
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
});
$(“div#output”).text(“mysite.com”+str);
//或者,如果您想要一个可点击的链接,而不仅仅是一个URL:
//$(“div#output”).append($(“”).attr({href:“mysite.com”+str}).append(“单击我”);
}
})
.触发(“变更”);

要仅在选择了所有三个选项后获取链接,只需使用
if
语句,仅在选择了3个选项时生成并显示链接。要使其成为URL,只需将获得的路径附加到基本URL即可。比如:

$("select").change(function () { 
  var str = "";
  if(#("select option:selected").length === 3){
    $("select option:selected").each(function () {
      var id = $(this).parent().attr('name');
      str += $(this).attr('value');             
    });
    $("div#output").text("mysite.com" + str);
    // Or, if you want a clickable link and not just a URL:
    // $("div#output").append($("<a>").attr({href: "mysite.com" + str}).append("Click me"));
  }
})
.trigger('change');
$(“选择”).change(函数(){
var str=“”;
如果(#(“选择选项:选定”)。长度==3){
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
});
$(“div#output”).text(“mysite.com”+str);
//或者,如果您想要一个可点击的链接,而不仅仅是一个URL:
//$(“div#output”).append($(“”).attr({href:“mysite.com”+str}).append(“单击我”);
}
})
.触发(“变更”);

您可以使用以下命令。添加
a
元素并将其隐藏,直到完成。使用所选选项更新
a
元素
href
属性:

$(“选择”).change(函数(){
var str=“”;
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
//使用选定值更新href
$(“#mySite”).attr(“href”,“mySite.com/”+str);
});
//保持锚元素隐藏,直到选择所有三个选项
$(“#mySite”)。切换($(“#cos”)。查找(“选项:选定”)。长度>0&$(“#颜色”)。查找(“选项:选定”)。长度>0&&$(“#年”)。查找(“选项:选定”)。长度>0);
$(“div#output”).text(str);
})
.触发(“变更”)
#mySite{
显示:无;
}

警察队伍
等级
紫黑色
红黑的
水黑
1984
1985
1986



您可以使用以下命令。添加
a
元素并将其隐藏,直到完成。使用所选选项更新
a
元素
href
属性:

$(“选择”).change(函数(){
var str=“”;
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
//使用选定值更新href
$(“#mySite”).attr(“href”,“mySite.com/”+str);
});
//保持锚元素隐藏,直到选择所有三个选项
$(“#mySite”)。切换($(“#cos”)。查找(“选项:选定”)。长度>0&$(“#颜色”)。查找(“选项:选定”)。长度>0&&$(“#年”)。查找(“选项:选定”)。长度>0);
$(“div#output”).text(str);
})
.触发(“变更”)
#mySite{
显示:无;
}

警察队伍
等级
紫黑色
红黑的
水黑
1984
1985
1986



您可以使用以下命令。添加
a
元素并将其隐藏,直到完成。使用所选选项更新
a
元素
href
属性:

$(“选择”).change(函数(){
var str=“”;
$(“选择选项:选定”)。每个(函数(){
var id=$(this.parent().attr('name');
str+=$(this.attr('value');
//使用选定值更新href
$(“#mySite”).attr(“href”,“mySite.com/”+str);
});
//保持锚元素隐藏,直到选择所有三个选项
$(“#mySite”)。切换($(“#cos”)。查找(“选项: