Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery get div';创建当前标签并填充_Javascript_Jquery_Asp.net Mvc 3 - Fatal编程技术网

Javascript Jquery get div';创建当前标签并填充

Javascript Jquery get div';创建当前标签并填充,javascript,jquery,asp.net-mvc-3,Javascript,Jquery,Asp.net Mvc 3,我有多个div,每个div都有一个文本框和一个标签。当textbox打开时,我需要在标签中填充一些文本 到目前为止的脚本: $(function () { $('input:text').focus(function () { //how do I get the current label and populate with some text? and when clicking on a //different textbox, I want to clear

我有多个div,每个div都有一个文本框和一个标签。当textbox打开时,我需要在标签中填充一些文本

到目前为止的脚本:

 $(function () {
   $('input:text').focus(function () {
     //how do I get the current label and populate with some text? and when clicking on a 
    //different textbox, I want to clear the previous label and just show the current
    //div's label with some data
 });
});
e、 g


@Html.TextBoxFor(model=>model.Title)
@Html.TextBoxFor(model=>model.Name)
@Html.TextBoxFor(model=>model.Address)
谢谢

您可以使用和:

尝试:

注意:最好按如下方式为div指定一个类名

<div class="parentDiv">
    <input type="text" id="Title" />
    <label></label>
</div>

这将避免其他div的标签变为空。

为您制作的工作示例:

HTML:


你真的,真的不需要发布截图。我们的浏览器可以很好地呈现页面。相信我,有时候确实会失败。我用大量代码回答了前面的问题,我浪费了时间格式化所有代码。放置实际代码的目的是让人们可以轻松地复制和粘贴它。也不能保证你的图像托管服务明天会在那里。你需要做的是学会使用内置的代码格式化工具。你知道吗?不要介意。我会为你做的。花了整整十二秒钟。下次我会打电话给你;-)无需提供div的id。使用“this”,您将获得当前焦点文本框。使用父选择器,您将获得当前焦点文本框div,然后使用find,我们将获得标签。因此,它不会反映在所有其他标签上。我认为您没有理解实际问题,也没有看到我的示例。如何清除答案中的其他标签。看看我的例子,试着理解我说的话。是的,那是我的错误。但仍然不需要为每个div指定id。我们可以通过为每个标签指定类名来轻松实现,在为标签指定值之前,我们将清除所有标签。例如,

在focus函数中,我们的第一行是$(“label.Clear”).val(“”)。同样的方法,我刚才说的是将类名(而不是id)分配给divs,您正在考虑标签的类名。
$(function () {
    $('input:text').focus(function () {
        $(this).siblings('label').empty().html('what you want..');
    });
});
var label = $(this).parent().find('label')
$('input:text').focus( function(){
    $(this).next().text('text here');
});
$('input:text').focus(function () {
   $('input:text').next('label').html(""); // reset all first
   $(this).next('label').html("Your text here");
});
$(function () {
   $('input:text').focus(function () {
     // To put textboxvalue in label
     $($(this).parent().find('label')).val($(this).val());
     // To put customvalue in label
     $($(this).parent().find('label')).val("Hi i am a label");
 });
});
$(function () {
   $('input:text').focus(function(e){
       $('div label').text("");
       $(this).next('label').text('SomeText');
    });
});​
<div class="parentDiv">
    <input type="text" id="Title" />
    <label></label>
</div>
       $('div.parentDiv label').text(""); 
       $(this).next('label').text('SomeText');
<div>
    <input type="text" title="Tittle" value=""/>
    <label>
    </label>
</div>
<div>
    <input type="text" title="Name" value=""/>
    <label>
    </label>
</div>
<div>
    <input type="text" title="Address" value=""/>
    <label>
    </label>
</div>​
$('input:text').focus(function () {
   // clean label
   $('input:text').next('label').html("");

   // get input title
   var title = $(this).attr('title');            

   // add html to label from input title
   $(this).next('label').html(title);
});​