我正在使用一个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>

有人能算出来吗?


当前回答

这招对我很管用:

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
                );
            });

其他回答

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

动态占位符的工作实例使用Javascript和Jquery http://jsfiddle.net/ogk2L14n/1/

<input type="text" id="textbox">
 <select id="selection" onchange="changeplh()">
     <option>one</option>
     <option>two</option>
     <option>three</option>
    </select>

function changeplh(){
    debugger;
 var sel = document.getElementById("selection");
    var textbx = document.getElementById("textbox");
    var indexe = sel.selectedIndex;

    if(indexe == 0) { 
     $("#textbox").attr("placeholder", "age");

}
       if(indexe == 1) { 
     $("#textbox").attr("placeholder", "name");
}
}

你可以简单地使用

$("#yourtextboxid").attr("placeholder", "variable");

其中,如果变量是字符串,那么你可以像上面那样使用,如果它是变量,用“变量”这样的名字替换它,不带双引号。

如:$ (" # youtextboxid ")。attr(“占位符”,变量); 它会起作用的。

如果输入是在div内部,那么你可以尝试这样做:

$('#div_id input').attr("placeholder", "placeholder text");

试试这个

$('#Selector_ID').attr("placeholder", "your Placeholder");