html 表单请填写此字段,将输入提示添加到HTML表单字段中

我遇到了同样的问题,但是使用IE 8不允许我选择HTML 5

我使用了受Kyle Schaeffer启发的以下代码。

$.fn.fieldPrompt = function () {

/*Add the two CSS elements to the application css file. They control the wrapping element and the prompt element

.prompt_element {display:inline-block; position:relative; }

.prompt_element .prompt_field {display:inline-block; color:#888; font-style:italic; position:absolute; left:5px; top:2px; }*/

var $this = $(this);

$(\'input[type=text][title],input[type=password][title],textarea[title]\', $this).each(function (i) {

if ($(this).parent().hasClass(\'prompt_element\') == false) { //if prompt already exists then skip

$(this).wrap(\'

_promptfieldClassName = \'prompt_\' + $(this)[0].uniqueID;

var _promptfield = \'

\' + $(this).attr(\'title\') + \'
\' //Create the prompt field

$(this).before(_promptfield) // Add the prompt field to the Prompt element. The

if ($.trim($(this).val()) != \'\') { //Check if the field has a value

$(this).prev().hide(); //Hide the prompt if field has a value

};

$(\'.prompt_field\').focus(function () { //If the prompt field get the focus - move to the next field which should be the input

$(this).next().focus();

});

$(this).on(\'keypress paste\', function () { //If field has keypress or paste event was triggered

$(this).prev().hide(); //hide the prompt field

});

$(this).blur(function () { //If leaving the field element

if ($.trim($(this).val()) == \'\') { //Check if the value is empty

$(this).prev().show(); //Show the prompt

}

else {

$(this).prev().hide(); //Hide the prompt. This can be initiated by other events if they fill the field.

}

});

};

});

return $(this);

}

使用Jquery自动完成功能时,我只需要添加$(this).blur();即可。更改选项功能的声明。这样可以确保在所有其他自动完成事件完成之后触发模糊事件,以确保对字段进行检查以在需要时重置提示。

$(...).autocomplete({change:function(){ $(this).blur(); }})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在HTML输入表单选择一张数据表的字段进行填充,可以使用一些JavaScript代码来实现。 首先,需要在HTML创建一个输入表单,并为其指定一个唯一的标识符,例如: ```html <form id="myForm"> <label for="input1">输入框1:</label> <input type="text" id="input1" name="input1"><br> <label for="input2">输入框2:</label> <input type="text" id="input2" name="input2"><br> <label for="field">选择字段:</label> <select id="field"> <option value="">选择字段</option> <option value="字段1">字段1</option> <option value="字段2">字段2</option> <option value="字段3">字段3</option> <!-- 其他选项 --> </select> </form> ``` 上述代码,我们创建了一个输入表单,并为其指定了一个唯一的标识符`myForm`。在表单,我们创建了两个输入框`input1`和`input2`,以及一个下拉选项`field`,用于选择数据表的字段。 接下来,我们可以使用JavaScript代码来实现在选择数据表字段时,自动填充所选字段对应的值的功能。例如: ```javascript // 获取表单元素 var form = document.getElementById("myForm"); // 获取选择字段的下拉选项 var field = form.elements["field"]; // 监听选项选择事件 field.addEventListener("change", function() { // 获取选字段名 var fieldName = field.value; // 根据字段名填充相应的输入框 fillInput(fieldName); }); // 填充输入框的方法 function fillInput(fieldName) { // 根据字段名查询字段对应的值 var fieldValue = queryFieldValue(fieldName); // 将字段值填充到相应的输入 if (fieldName == "字段1") { form.elements["input1"].value = fieldValue; } else if (fieldName == "字段2") { form.elements["input2"].value = fieldValue; } // 其他字段对应的输入框 } // 查询字段值的方法 function queryFieldValue(fieldName) { // 根据字段名查询字段对应的值,例如从数据库查询,或者使用一些预定义的映射关系等等。 // 这里只是一个示例,返回一个固定的字符串。 return "字段" + fieldName.substr(2) + "的值"; } ``` 上述代码,我们首先获取了表单元素,并使用`form.elements`属性获取了选择字段的下拉选项。然后,我们使用`addEventListener`方法监听了选项选择事件,在事件处理函数获取了选字段名,并调用了`fillInput`方法来填充对应的输入框。 在`fillInput`方法,我们使用`queryFieldValue`方法来查询字段对应的值,并将其填充到相应的输入。我们使用了`form.elements`属性来获取输入框元素,并使用`value`属性来设置输入框的值。 在`queryFieldValue`方法,我们可以根据字段名查询对应的字段值,例如从数据库查询,或者使用一些预定义的映射关系等等。这里只是一个示例,返回一个固定的字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值