jQuery readOnly奇怪的行为 [英] JQuery readOnly strange behavior

查看:63
本文介绍了jQuery readOnly奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

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>

使用此脚本:

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

我的问题是:调用makeReadOnly(false)可以,而makeReadOnly('false')不能.

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

但是makeReadOnly('true')可行.

(已通过JQuery 1.9.1测试)

(tested with JQuery 1.9.1)

谁能解释我为什么?

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

推荐答案

由于强制转换.字符串"false"被强制转换为布尔值true.基本上,任何非空字符串都是(以及0和其他一些东西).试试吧! Boolean("false").

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").

readOnly属性将其输入强制转换为布尔值.因此,"true""false"都变为true,只有false保持false.

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

这篇关于jQuery readOnly奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆