删除readonly属性并更改类型并将焦点放在单个事件中的相同输入字段中

时间:2014-09-16 09:02:19

标签: android jquery ios jquery-mobile cordova

我正在使用jquery mobile for cordova应用程序在android和ios中运行.. 我有一个要求,我有一个文本字段,其默认值为readonly状态。在聚焦文本字段时,我需要删除值和readonly属性,并将类型更改为datetime-local和focus。

所有这些都应该在一次触摸事件中发生。现在关注readonly,输入字段的值和类型会改变。但是没有聚焦,所以必须再次单击以查看android和ios设备中的日期时间选择器。我找不到问题。

我的代码是这样的。

HTML:

<input type="text" readonly data-clear-btn="false" id="instantTime"  value="Right Now" >

JS:

$('#instantTime').off("focus").on("focus", function(e) {
        e.preventDefault();
        if($(this).prop("type") == "text"){
            $(this).removeAttr('readonly').val('');
            $(this).prop("type", 'datetime-local').select();
        }
    });
$('#instantTime').off("blur").on("blur", function(e) {
    e.preventDefault();
    if($(this).val().trim() == ""){
        $(this).prop("type",'text').val("Right Now").attr('readonly', true).blur();
    }else{
        $(this).prop("type",'datetime-local').blur();
    }
});

提前致谢。

1 个答案:

答案 0 :(得分:0)

我无法谈论android。但以下是 HTML

的等效内容

将以下代码放入 $(document).ready(function(){})

$("#instantTime").click(function() {
    $(this).prop("readonly",false);
    $(this).prop("type","datetime-local");
    $(this).focus();
});