开发者

ASP.NET input tags - cursor:default

开发者 https://www.devze.com 2023-03-27 01:38 出处:网络
I\'ve got a page with a couple dozen (dynamically changeable amount) input fields (mostly text, a few buttons), and when the form is complete/submitted/finalized, it can no longer be edited but can st

I've got a page with a couple dozen (dynamically changeable amount) input fields (mostly text, a few buttons), and when the form is complete/submitted/finalized, it can no longer be edited but can still be viewed. What I did to accomplish this is to declare a string called disabled, and if the form has been finalized, it is set as

disabled="disabled=\"disabled\""

and otherwise is null. I then place it in evey input tag, so that they will be disabled. This works fine. However, the cursor still changes to a pointer on the buttons and a text cursor on the text boxes. How can I change that? I'm trying with

string disabled = (bool)ViewData["Finalized"] ? "disabled = \"disabled\" style=\"cursor:default\"" : null; 

which renders the quotes within the tags as ". The disable feature works fine, but the cursor is unaffacted. I've tried rewriting it with single quotes within the string (same result), and with single quotes around the string and double quotes within it (error, too many characters in literal), but have had no success. Is this something that can be done this way? Or am I going about it in the wrong way?

The resulting output of the above setup is

<input maxlength="4" type="text" name="input1" value="1"  disabled = &quot;disabled&quot; style=&quot;cursor:default&quot;/>

Forgot to mention, inputs are defined on the page开发者_如何学编程 along the lines of this example:

<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%: disabled %> />


So the output is exactly this?

<input maxlength="4" type="text" name="input1" value="1"  disabled = &quot;disabled&quot; style=&quot;cursor:default&quot;/>

You disabled string is getting html encoded when it's set on the element. The reason is the way you're setting it onto the element.

Change this:

<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%: disabled %> />

To this:

<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%= disabled %> />

... and you should be good. The reason is that <%: disabled %> html encodes and <%= disabled %> does not.


When you render any server variable along with the markup in the view you should use MvcHtmlString.Create method. It will not encode the special characters when it renders. Try this it will solve your problem.

<input type="button" id="newtablebutton" class="pctbutton white" value="Add Table" onclick="showbox()" <%= MvcHtmlString.Create(disabled) %> />


Try using cursor:none when you don't want the cursor to display.

Try adding something to this effect to your disabled string:

onfocus="javascript:if (this.disabled) this.style.cursor = 'none';


You might also try setting the input as readonly too, along with it being disabled. I don't know if this will effect the cursor, but it may. Definitely simpler than a jQuery or JS solution.


When in doubt, use jQuery!

function hideCursor() {
   $("input[disabled='disabled']").css("cursor","default");
}

Just call that whenever you want.

Also to make life simpler:

function disableForm() {
    $("#myForm input").attr('disabled', 'disabled');
    $("#myForm input").css("cursor","default");
}

Cheers!


How about you add this to your CSS instead of trying to generate the style on each element?

input[disabled]
{
    cursor: default;
}

(Note that in my tests Chrome would already use the default cursor on a disabled input)

0

精彩评论

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