如何创建代码window.location.href
以准确提供value
的{{1}}:
<option
示例浏览器网址:http://www.test.com/page8要选择:
<script type="text/javascript">
jQuery(document).ready(function($){
$('select').find('option[value=window.location.href]').attr('selected','selected');
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select>
<option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8">United States dollar (USD)</option>
</select>
答案 0 :(得分:0)
试试这个:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var url = $('select').attr('selected','selected').val();
alert(url);
});
</script>
<select>
<option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8" selected="selected">United States dollar (USD)</option>
</select>
答案 1 :(得分:0)
window.location.href
是位置对象的javascript属性,因此需要将其连接到值字符串,以便在字符串中应用其值,并且最好引用该值:
jQuery(document).ready(function($){
$('select').find('option[value="'+ window.location.href+'"]')
.attr('selected','selected');
});
示例:
var locationHrefMock = "http://www.test.com/page5" ;
jQuery(document).ready(function($){
$('select').find('option[value="'+ locationHrefMock +'"]')
.attr('selected','selected');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8">United States dollar (USD)</option>
</select>