Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following HTML code:

<input type="text" id="test" ><br>
<button onclick="makeReadOnly('true')">Make read only (ok)</button>
<button onclick="makeReadOnly('false')">Make read/write (not ok)</button> 
<button onclick="makeReadOnly(false)">Make read/write (ok)</button>

with this script:

function makeReadOnly(r) {
    $("#test").prop("readOnly", r);

}

My problem is: call to makeReadOnly(false) works, while makeReadOnly('false') doesn't.

But makeReadOnly('true') works.

(tested with JQuery 1.9.1)

Who can explain me why ?

http://jsfiddle.net/ym9qe99p/1/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

Because of casting. The string "false" is cast as a Boolean true. Basically, any non-empty string is (along with 0 and a couple other things). Try it! Boolean("false").

The readOnly property casts its input to a Boolean. Therefore both "true" and "false" become true, and only false stays false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...