开发者

how to prevent select item in dropdowlist using javascript?

I want prevent user to select dropdown list items using javascript methods. Is there anyway use OnMouseKeyDown so I can stop there. I donot want use Enable=false.

开发者_JAVA技巧

Javascripts gurus, help me out.


If you want the user to be able to open the select box but not change the selection, you can do the following:

<select onchange="this[0].selected = true">
<option>1</option>
<option>2</option>
<option>3</option>
</select>

Where you change the selected index to the item you wish to keep selected. As a side note, this usually isn't a very good idea from a user interface perspective.


let's try this in c#

string hardcode = "1"; // set as appropriate
myDropDownList.Attributes["onchange"] = "this.selectedIndex = " + hardcode + ";";

keep in mind if your user has javascript disabled this isn't going to work, and you should be doing verification on the server no matter what.


Using jquery

HTML:

<select id="dropdown">
   <option>Select an option</option>
   <option>Option 1</option>
   <option>Option 2</option>
   <option>Option 3</option>
 </select>

JS:

$(function(){
    $("#dropdown").change(function(){
        $(this).find("option:first").attr("selected", "selected");
        return false;
    });
});


onfocus="this.blur()"

add that to the element

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜