我使用requiredfieldvalidator
作为一些文本框,每个文本框都有占位符属性,但是当我点击提交按钮时,验证器没有触发,这个问题出现在IE中,因为IE默认情况下不支持占位符所以我使用Jquery这个目的,这是我的代码:
Jquery的:
<script type="text/javascript">
$(document).ready(function () {
// $("#txtAddress").removeAttr("style");
var userAgent = navigator.userAgent.toString().toLowerCase();
if (userAgent.indexOf('ie') != -1) {
$('[placeholder]').focus(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
}
});
</script>
天冬氨酸:
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
<p>
<asp:TextBox ID="txtOrgName" runat="server" placeholder="ناوی دامەزراوە"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="red" CssClass="validator" ControlToValidate="txtOrgName" ErrorMessage="*"></asp:RequiredFieldValidator>
</p>
<br/>
<p>
<asp:TextBox ID="txtPhone" runat="server" placeholder="تەلەفۆن"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" CssClass="validator" ControlToValidate="txtPhone" ForeColor="red" ErrorMessage="*"></asp:RequiredFieldValidator>
</p>
<br/>
<p>
<asp:TextBox ID="txtMobile" runat="server" placeholder="مۆبایل"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtMobile" CssClass="validator" ForeColor="red" ErrorMessage="*"></asp:RequiredFieldValidator>
</p>
<br/>
<p>
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Height="120px" Width="200px" style="direction: ltr;" placeholder="ناونیشان">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtAddress" CssClass="validator" ForeColor="red" ErrorMessage="*"></asp:RequiredFieldValidator>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="تۆمارکردن" />
</p>
答案 0 :(得分:0)
$(document).ready(function () {
if ($.browser.msie === true) {
$('#formId').find('[placeholder]').focus(function () {
var inputObjObj = $(this);
if (inputObj.val() == inputObj.attr('placeholder')) {
inputObj.val('').removeClass('placeholder');
}
}).blur(function () {
var inputObj = $(this);
if (inputObj.val() == '' || inputObj.val() == inputObj.attr('placeholder')) {
inputObj.addClass('placeholder').val(inputObj.attr('placeholder'));
}
}).blur();
$('#formId').find('#Button1').click(function () {
$(this).parents('form').find('[placeholder]').each(function (i) {
var inputObj = $(this);
if (inputObj.val() == inputObj.attr('placeholder')) {
inputObj.val('').removeClass('placeholder');
}
});
if (Page_ClientValidate()) {
//Form is valid;
} else {
//Form is not valid;
return false;
}
});
}
});