Javascript 在“选择”下拉列表中自动设置默认文本
我有一个非常简单的select下拉列表,其中包含将用户指向相应页面的URLJavascript 在“选择”下拉列表中自动设置默认文本,javascript,jquery,html,selection,Javascript,Jquery,Html,Selection,我有一个非常简单的select下拉列表,其中包含将用户指向相应页面的URL <select> <option value="url1">title1 </option> <option value="url2">title2 </option> <option value="url3">title3 </option> ........
<select>
<option value="url1">title1 </option>
<option value="url2">title2 </option>
<option value="url3">title3 </option>
.........
</select>
标题1
标题2
标题3
.........
我将在所有这些(url1、url2、url3…)中使用此下拉列表作为导航。是否可以根据我的URL在选择框中设置默认文本?假设我当前在url2上,选择框中的默认文本将是title2。我知道你可以用
<option selected="selected" value="url2">title2</option>
标题2
但是有没有一种方法我可以使用javascript来做,因为我有数百个页面?所有的URL和标题都存储在我可以检索的数组中
谢谢你的帮助 说你有
<form name="MyForm">
<select name="SelectBox1">
<option>One
<option>Two
<option>Three
</select>
或者使用如下函数将其设置为值。。传入选择字段名及其应为的值
function setSelect(sFieldName, sValue) {
var el=document.getElementsByName(sFieldName)[0] // returns array of all elements with that name so use [0] to get 1st one
for (var i=0;i<el.options.length;
if (el.options[i].value == sValue) { // if they match...
el.selectedIndex=i; // then this should be the default
}
}
说你有
<form name="MyForm">
<select name="SelectBox1">
<option>One
<option>Two
<option>Three
</select>
或者使用如下函数将其设置为值。。传入选择字段名及其应为的值
function setSelect(sFieldName, sValue) {
var el=document.getElementsByName(sFieldName)[0] // returns array of all elements with that name so use [0] to get 1st one
for (var i=0;i<el.options.length;
if (el.options[i].value == sValue) { // if they match...
el.selectedIndex=i; // then this should be the default
}
}
说你有
<form name="MyForm">
<select name="SelectBox1">
<option>One
<option>Two
<option>Three
</select>
或者使用如下函数将其设置为值。。传入选择字段名及其应为的值
function setSelect(sFieldName, sValue) {
var el=document.getElementsByName(sFieldName)[0] // returns array of all elements with that name so use [0] to get 1st one
for (var i=0;i<el.options.length;
if (el.options[i].value == sValue) { // if they match...
el.selectedIndex=i; // then this should be the default
}
}
说你有
<form name="MyForm">
<select name="SelectBox1">
<option>One
<option>Two
<option>Three
</select>
或者使用如下函数将其设置为值。。传入选择字段名及其应为的值
function setSelect(sFieldName, sValue) {
var el=document.getElementsByName(sFieldName)[0] // returns array of all elements with that name so use [0] to get 1st one
for (var i=0;i<el.options.length;
if (el.options[i].value == sValue) { // if they match...
el.selectedIndex=i; // then this should be the default
}
}
您应该能够使用以下功能:
var path = decodeURIComponent(window.location.pathname.replace(/\/$/, ""));
$("option").each(function () {
var url = $(this).val();
if (path.substring(0, url.length) === url) {
$(this).prop('selected', true);
}
});
路径是URL的结尾。下一段代码循环遍历选项元素,查看选项值是否与路径匹配,如果匹配,则将所选属性设置为true。您应该能够使用以下选项:
var path = decodeURIComponent(window.location.pathname.replace(/\/$/, ""));
$("option").each(function () {
var url = $(this).val();
if (path.substring(0, url.length) === url) {
$(this).prop('selected', true);
}
});
路径是URL的结尾。下一段代码循环遍历选项元素,查看选项值是否与路径匹配,如果匹配,则将所选属性设置为true。您应该能够使用以下选项:
var path = decodeURIComponent(window.location.pathname.replace(/\/$/, ""));
$("option").each(function () {
var url = $(this).val();
if (path.substring(0, url.length) === url) {
$(this).prop('selected', true);
}
});
路径是URL的结尾。下一段代码循环遍历选项元素,查看选项值是否与路径匹配,如果匹配,则将所选属性设置为true。您应该能够使用以下选项:
var path = decodeURIComponent(window.location.pathname.replace(/\/$/, ""));
$("option").each(function () {
var url = $(this).val();
if (path.substring(0, url.length) === url) {
$(this).prop('selected', true);
}
});
路径是URL的结尾。下一个代码块循环遍历选项元素,查看选项值是否与路径匹配,如果匹配,则将所选属性设置为true。您可以使用
document.URL
获取当前URL,并在可以使用的document ready上
$("#selectId option[value=" + document.URL + "]").prop('selected', true);
但是,document.URL
包含完整路径,因此如果它不在select的value
中,则需要截断不必要的部分,如http://https:/
还有,这里是
另外,小提琴只能第二次演奏。它第一次显示了不同的URL。必须是JSFIDLE的私人物品。您可以使用
文档获取当前URL。URL
并在您可以使用的文档上
$("#selectId option[value=" + document.URL + "]").prop('selected', true);
但是,document.URL
包含完整路径,因此如果它不在select的value
中,则需要截断不必要的部分,如http://https:/
还有,这里是
另外,小提琴只能第二次演奏。它第一次显示了不同的URL。必须是JSFIDLE的私人物品。您可以使用
文档获取当前URL。URL
并在您可以使用的文档上
$("#selectId option[value=" + document.URL + "]").prop('selected', true);
但是,document.URL
包含完整路径,因此如果它不在select的value
中,则需要截断不必要的部分,如http://https:/
还有,这里是
另外,小提琴只能第二次演奏。它第一次显示了不同的URL。必须是JSFIDLE的私人物品。您可以使用
文档获取当前URL。URL
并在您可以使用的文档上
$("#selectId option[value=" + document.URL + "]").prop('selected', true);
但是,document.URL
包含完整路径,因此如果它不在select的value
中,则需要截断不必要的部分,如http://https:/
还有,这里是
另外,小提琴只能第二次演奏。它第一次显示了不同的URL。必须是JSFIDLE个人的事情。假设您希望匹配窗口位置中的url(例如
http://www.example.com/some/page.html
)中找到的网页的URL:
var dropdown = document.getElementById( 'dropdown' );
for ( var i = 0; i < dropdown.childElementCount; ++i ) {
if ( dropdown.children[i].value === document.location.href) {
dropdown.selectedIndex = i;
break;
}
}
var dropdown=document.getElementById('dropdown');
对于(变量i=0;i
其中
'dropdown'
包含
元素的ID。jsiddle:假设您希望匹配窗口位置中的url(例如http://www.example.com/some/page.html
)中找到的网页的URL:
var dropdown = document.getElementById( 'dropdown' );
for ( var i = 0; i < dropdown.childElementCount; ++i ) {
if ( dropdown.children[i].value === document.location.href) {
dropdown.selectedIndex = i;
break;
}
}
var dropdown=document.getElementById('dropdown');
对于(变量i=0;i
其中
'dropdown'
包含
元素的ID。jsiddle:假设您希望匹配窗口位置中的url(例如http://www.example.com/some/page.html
)中找到的网页的URL:
var dropdown = document.getElementById( 'dropdown' );
for ( var i = 0; i < dropdown.childElementCount; ++i ) {
if ( dropdown.children[i].value === document.location.href) {
dropdown.selectedIndex = i;
break;
}
}
var dropdown=document.getElementById('dropdown');
对于(变量i=0;i
其中
'dropdown'
包含
元素的ID。jsiddle:假设您希望匹配窗口位置中的url(例如http://www.example.com/some/page.html
)中找到的网页的URL:
var dropdown = document.getElementById( 'dropdown' );
for ( var i = 0; i < dropdown.childElementCount; ++i ) {
if ( dropdown.children[i].value === document.location.href) {
dropdown.selectedIndex = i;
break;
}
}
var dropdown=document.getElementById('dropdown');
对于(变量i=0;i
其中“下拉列表”
包含