HTML表单“占位符”在IE 8中不起作用

时间:2012-07-02 15:24:24

标签: html forms html5 internet-explorer placeholder

您好我在我的网站上创建了一个表单。我正在使用placeholder="Full Name",但在IE8(以及可能的其他IE版本)中查看时,表单中没有任何内容出现

我尝试过使用value="" onfocus="if (this.value == 'Full Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Full Name';}",但表单仍然是空的。出于某种原因,当您单击表单时会显示“全名”,然后将其关闭。

我正在研究的网站是[usspcatalystcentre.org.uk] [1],你可以看到我的意思。前两个表单(标题和全名)是我尝试使用

的地方
value="" onfocus="if (this.value == 'Full Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Full Name';}" 


表单的其余部分是我刚刚使用placeholder

的地方

以下是我正在处理的实际代码

<form action="contact.php" method="post">


                <input id=title name=title type=text value="" onfocus="if(this.value=='Username') this.value='';" onblur="if(this.value=='') this.value='Username';" required class="top-formfields" value='<?php print $_SESSION['title']?>'>  <br /><br />
                <input id=fullname name=fullname type=text value="" onfocus="if(this.value=='Full Name') this.value='';" onblur="if(this.value=='') this.value='Full Name';" required  class="top-formfields" value='<?php print $_SESSION['fullname']?>'> <br /><br />
                <input id=companyname name=companyname type=text placeholder="Company Name" required  class="top-formfields" value='<?php print $_SESSION['companyname']?>'>    <br /><br />
                <input id=companynumber name=companynumber type=text placeholder="Company Registered Number" required  class="top-formfields" value='<?php print $_SESSION['companynumber']?>'> <br /><br />
                <textarea id=address name=address rows=5 placeholder="Address" required class="top-textarea"><?php print $_SESSION['address']?></textarea> <br /><br />
                <input id=postcode name=postcode type=text placeholder="Post Code" required  class="top-formfields" value='<?php print $_SESSION['postcode']?>'>    <br /><br />
                <input id=phonenumber name=phonenumber type=text placeholder="Phone Number" required  class="top-formfields" value='<?php print $_SESSION['phonenumber']?>'>    <br /><br />
                <input id=email name=email type=text placeholder="Email" required  class="top-formfields" value='<?php print $_SESSION['email']?>'> <br /><br />
                <input id=mobile name=mobile type=text placeholder="Mobile" required  class="top-formfields" value='<?php print $_SESSION['mobile']?>'> <br /><br />
                <input id=website name=website type=text placeholder="Website URL" required  class="top-formfields" value='<?php print $_SESSION['website']?>'> <br /><br />

先谢谢,汤姆

6 个答案:

答案 0 :(得分:6)

placeholder属性是一项新的HTML5改进。旧的IE-s不支持它 - http://diveintohtml5.info/forms.html

要模仿跨浏览器,您可能需要使用jQuery - example

答案 1 :(得分:3)

IE-9不支持占位符,所以我想到的第一个解决方案就是这个。

祝你好运!

<script type="text/javascript"> 
    if(navigator.userAgent.indexOf("MSIE") > 0 )
    {
        $(function()
        {
            var input = document.createElement("input");
            if(('placeholder' in input) == false)
            {
                $('[placeholder]').focus(function()
                {
                    var i = $(this);
                    if(i.val() == i.attr('placeholder'))
                    {
                        i.val('').removeClass('placeholder');
                        if(i.hasClass('password'))
                        {
                            i.removeClass('password'); this.type='password';
                        }
                    }
                }).blur(function()
                {
                    var i = $(this);
                    if(i.val() == '' || i.val() == i.attr('placeholder'))
                    {
                        if(this.type=='password')
                        {
                            i.addClass('password'); this.type='text';
                        }
                        i.addClass('placeholder').val(i.attr('placeholder'));
                    }
                }).blur().parents('form').submit(function()
                {
                    $(this).find('[placeholder]').each(function()
                    {
                        var i = $(this);
                        if(i.val() == i.attr('placeholder')) i.val('');
                    });
                });
            }
        });
    }


    </script>

答案 2 :(得分:0)

Tom,占位符不是IE8支持的属性。

我认为这可以帮助您实现目标:

使用“全名”预填充字段。提前给它值=“全名”作为默认值。

onfocus= "if (this.value=='Full Name'){this.select();this.value='';this.style.color='#999999';}else{this.style.color='#000000';}"  

onclick= "if (this.value=='Full Name'){this.select();this.value='';this.style.color='#999999';}else{this.style.color='#000000';}" 

onblur=  "if (this.value==''||this.value=='Full Name'){this.value='Full Name';this.style.color='#999999';}else{this.style.color='#000000';}" 

onkeyup= "if (this.value=='Full Name'){this.select();this.style.color='#999999';}else{this.style.color='#000000';}" 

onchange="if (this.value=='Full Name'){this.style.color='#999999';}else{this.style.color='#000000';}"

答案 3 :(得分:0)

if (!('placeholder' in $('<input>')[0])) { // Create a dummy element for feature detection
    $('input[placeholder], textarea[placeholder]').blur(add).focus(remove).each(add); // Select the elements that have a placeholder attribute
    $('form').submit(function(){$(this).find('input[placeholder], textarea[placeholder]').each(remove);}); // Remove the placeholder text before the form is submitted
}

答案 4 :(得分:0)

将以下代码保存为.js,并确保更改输入类型

$(document).ready(function () {
    if ($.browser.msie) { //this is for only ie condition ( microsoft internet explore )
        $('input[type="text"][placeholder], textarea[placeholder]').each(function () {
            var obj = $(this);
            if (obj.attr('placeholder') != '') {
                obj.addClass('IePlaceHolder');

                if ($.trim(obj.val()) == '' && obj.attr('type') != 'password') {
                    obj.val(obj.attr('placeholder'));
                }
            }
        });

        $('.IePlaceHolder').focus(function () {
            var obj = $(this);
            if (obj.val() == obj.attr('placeholder')) {
                obj.val('');
            }
        });

        $('.IePlaceHolder').blur(function () {
            var obj = $(this);
            if ($.trim(obj.val()) == '') {
                obj.val(obj.attr('placeholder'));
            }
        });
    }
});

http://jsfiddle.net/wbcTm/79/

答案 5 :(得分:0)

如果您使用的是jQuery 1.9+,请务必添加浏览器检测。您可以使用此命令从终端安装npm install jquery.browser