HTML表单 - 电话相关值不会在焦点上消失

时间:2013-09-26 11:50:57

标签: javascript html forms input

嗨我有一个html表单,我有一些javascript运行每次你关注输入字段rel值消失,但它正在处理除电话以外的所有输入类型,我不知道我做错了什么。

javascript是:

$(document).ready(function() {

    $('select.fancyselect').fancySelect();

    $('input[type=text], input[type=email], input[type=phone], input[type=url], textarea,').focus(function(){ 
        if($(this).val() == $(this).attr('rel')) {
            $(this).val('');
        }
    });

    $('input[type=text], input[type=email], input[type=phone], input[type=url], textarea').blur(function(){
        if($(this).val() == '') {
            $(this).val($(this).attr('rel'));
        } 
    });

电话表单字段的html是:

<div class="control-group">
                                <label class="control-label" for="field-phone">Phone Number:<br /> <em>(Optional)</em></label>
                            <div class="controls">
                                <input type="number" name="field-phone" id="field-phone" value="<?= $_POST['field-phone']; ?>" rel="Phone number here...">
                            </div>
                          </div>

我正在使用bootstrap,我发现这个问题只发生在firefox而不是chrome或safari中。

2 个答案:

答案 0 :(得分:1)

您使用的CSS选择器input[type=phone]引用了您没有的<input type='phone'/>。您正在使用<input type='number'/>,因此您需要使用CSS选择器input[type='number']

答案 1 :(得分:0)

试试这个:

<input type="text" onfocus="if(this.value == '<?= $_POST['field-phone']; ?>') { this.value = ''; }" value="<?= $_POST['field-phone']; ?>" />