函数jQuery菜单选择url浏览器

时间:2016-10-19 05:02:36

标签: jquery

如何创建代码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>

2 个答案:

答案 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>