开发者

determine input element status

开发者 https://www.devze.com 2023-04-03 19:55 出处:网络
I have a problem determining status of an input field. I want JS to determine whether it is disabled or enabled but my code below always return \'false\'. What am I doing wrong? Thanks

I have a problem determining status of an input field. I want JS to determine whether it is disabled or enabled but my code below always return 'false'. What am I doing wrong? Thanks

   <input name="ctl00$ctl00$ctl00$ctl00$ContentPlaceHolderDefault$RobinBodyPlaceHolder   $LoggedInBodyPlaceHolder$AddEditUs开发者_运维问答er1$EmailTextBox" type="text" value="email@mail.com"    id="ContentPlaceHolderDefault_RobinBodyPlaceHolder_LoggedInBodyPlaceHolder_AddEditUser1_EmailTextBox"    class="EmailTextBox" disabled="disabled"/>


function ConfirmEmailChange() {
    alert($('.EmailTextBox').disabled==true);
}


You're trying to access the disabled property of a jQuery object (I'm assuming it's jQuery, but it could be some other JS library using the $ character), but jQuery objects don't have a disabled property.

If it is jQuery, you can access the actual DOM node contained within the jQuery object using array notation:

alert($('.EmailTextBox')[0].disabled);

Alternatively, you can use the get method:

alert($('.EmailTextBox').get(0).disabled);

Or, as others have shown, you can use the jQuery prop method. Notice that I've removed the == true part, as disabled is a boolean property and will return true or false anyway.


The question isn't tagged jQuery, but since you seem to be using it, you can do this:

alert($('.EmailTextBox').is(':disabled'))


Live Demo

function ConfirmEmailChange() {
    alert($('.EmailTextBox').prop('disabled'));
}

Just change it to check the disabled property this way. You were trying to access .disabled which isn't a valid jQuery property.

0

精彩评论

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