我正在使用一个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>
有人能算出来吗?
把你的第一行移到底部对我有用: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();
});
$(this).val()是一个字符串。使用parseInt($(this).val(), 10)或检查'1'。10表示以10为底。
$(function () {
$('input[placeholder], textarea[placeholder]').blur();
$('#serMemdd').change(function () {
var k = $(this).val();
if (k == '1') {
$("#serMemtb").attr("placeholder", "Type a name (Lastname, Firstname)").blur();
}
else if (k == '2') {
$("#serMemtb").attr("placeholder", "Type an ID").blur();
}
else if (k == '3') {
$("#serMemtb").attr("placeholder", "Type a Location").blur();
}
});
});
Or
$(function () {
$('input[placeholder], textarea[placeholder]').placeholder();
$('#serMemdd').change(function () {
var k = parseInt($(this).val(), 10);
if (k == 1) {
$("#serMemtb").attr("placeholder", "Type a name (Lastname, Firstname)").blur();
}
else if (k == 2) {
$("#serMemtb").attr("placeholder", "Type an ID").blur();
}
else if (k == 3) {
$("#serMemtb").attr("placeholder", "Type a Location").blur();
}
});
});
其他插件
ori已经让我注意到你正在使用的插件不能克服ie HTML失败。
试试这样做:
http://archive.plugins.jquery.com/project/input-placeholder