我正在使用一个jQuery占位符插件(https://github.com/danielstocks/jQuery-Placeholder)。我需要在下拉菜单中更改占位符文本。但这并没有改变。代码如下:

$(function () {
    $('input[placeholder], textarea[placeholder]').placeholder();
    $('#serMemdd').change(function () {
        var k = $(this).val();
        if (k == 1) {
            $("#serMemtb").attr("placeholder", "Type a name (Lastname, Firstname)").placeholder();
        }
        else if (k == 2) {
            $("#serMemtb").attr("placeholder", "Type an ID").placeholder();
        }
        else if (k == 3) {
            $("#serMemtb").attr("placeholder", "Type a Location").placeholder();
        }
    });
});

我的Html:

<div class="filterbox">
        <select name="ddselect" id="serMemdd">
            <option value="1" selected="selected">Search by Name</option>
            <option value="2">Search by ID</option>
            <option value="3">Search by Location</option>
        </select>
        <input id="serMemtb" type="text" style="width: 490px" placeholder="Type a name    (Lastname, Firstname)" />
        <input id="seMemBut" type="button" value="Search" />
    </div>

有人能算出来吗?


当前回答

$(document).ready(function(){ 
     $("input[type=search]").attr("placeholder","this is a test");
});

其他回答

$(document).ready(function(){ 
     $("input[type=search]").attr("placeholder","this is a test");
});

把你的第一行移到底部对我有用:http://jsfiddle.net/tcloninger/SEmNX/

$(function () {
    $('#serMemdd').change(function () {
        var k = $(this).val();
        if (k == 1) {
            $("#serMemtb").attr("placeholder", "Type a name (Lastname, Firstname)").placeholder();
        }
        else if (k == 2) {
            $("#serMemtb").attr("placeholder", "Type an ID").placeholder();
        }
        else if (k == 3) {
            $("#serMemtb").attr("placeholder", "Type a Location").placeholder();
        }
    });
    $('input[placeholder], textarea[placeholder]').placeholder();
});

这个插件看起来不是很健壮。如果再次调用. Placeholder(),它会创建一个新的Placeholder实例,而事件仍然绑定到旧的实例。

看一下代码,你可以这样做:

$("#serMemtb").attr("placeholder", "Type a name (Lastname, Firstname)").blur();

编辑

占位符是HTML5属性,猜猜谁不支持它?

你的插件似乎并没有真正帮助你克服IE不支持它的事实,所以当我的解决方案起作用时,你的插件没有。你为什么不找个懂的。

$(document).ready(function(){ 
  $('form').find("input[type=search]").each(function(ev)
  {
     $(this).attr("placeholder", "Search Whatever you want");
  });
});

这招对我很管用:

jQuery('form').attr("placeholder","Wert eingeben");

但现在这个不管用了:

// Prioritize "important" elements on medium.
            skel.on('+medium -medium', function() {
                jQuery.prioritize(
                    '.important\\28 medium\\29',
                    skel.breakpoint('medium').active
                );
            });