English 中文(简体)
...... 在复习时,选择的选项有缺省值?
原标题:Is There a way to have the selected option be the default value upon refreshing?

今天我有这个问题,我希望我选择的办法是,在我一页复读时,不实值。 例如,我有:

<select id="options">
<option value="0">Apple</option> 
<option value="1">Orange</option> 
<option value="2">Mango</option></select>

我的“倡议”的违约是“ Apple”,因此,我想的是,当我点击“奥兰”方案时,那将是我重新上页时的违约价值。 是否有办法这样做? 增 编

问题回答

I think local storage can help to you. So for example:

<select id="options">
<option value="0">Apple</option> 
<option value="1">Orange</option> 
<option value="2">Mango</option></select>

Javascript

$("#options").on("change", function(){
    var val = $(this).val();
    // save to local
    if(window.localStorage){
        window.localStorage.setItem("#options-val", val);
    }
});

if(window.localStorage){
    var item = window.localStorage.getItem("#options-val");
    if(item) $("#options").val(item);
}

参看demo

你们的所有支部都必须在美元内(文件)。

After后,试图重载网页,你将看到结果!

换成会议时,可以选择。 页: 1

<select id="options" onChange="document.cookie= fruit= +this.value;">
<option value="0">Apple</option> 
<option value="1">Orange</option> 
<option value="2">Mango</option></select>

<script type="text/javascript">
<!--
function getCookie(cname)
{
/* http://www.w3schools.com/js/js_cookies.asp */
var name = cname + "=";
var ca = document.cookie.split( ; );
for(var i=0; i<ca.length; i++)
  {
  // var c = ca[i].trim(); // trim not supported on IE8
  var c = ca[i].replace(/^s+|s+$/gm,  );
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
return "";
} 
window.onload = function() {
    var v = getCookie( fruit );
    if (v) document.getElementById( options ).options[v].selected = true;
}
//-->
</script>

选择的“选择”是你想要的缺省。

<option selected="selected">
  default
</option>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签