jQuery 设置输入文本的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18425208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:36:20  来源:igfitidea点击:

Set the default value of an input text

javascriptjqueryhtml

提问by rocky

my requirement is to save the entire "html" inside a div, but when i load an "html" with text fields to a div and then editing the value of the text box, the newly set value doesn't reflect in the core "html". I tried to inspect the value with fire bug and it remains the same or no value at all.With "jquery" i tried to set attribute but no attribute name value is created. how can i set the value of text fields and then get that "html" with the newly set value.

我的要求是将整个“html”保存在 div 中,但是当我将带有文本字段的“html”加载到 div 中,然后编辑文本框的值时,新设置的值不会反映在核心中“ html”。我试图用 fire bug 检查该值,但它仍然保持不变或根本没有值。使用“jquery”,我尝试设置属性,但没有创建属性名称值。如何设置文本字段的值,然后使用新设置的值获取“html”。

here is my html

这是我的 html

<div class="sub_input_box">
  <input type="text" / class="boreder_line">
  <input type="text" id="txt" value=""/>
  <input type="hidden" id="hid" />
  <div class="clear"></div>
</div>

and the jquery i used to set attribute

和我用来设置属性的 jquery

$("#txt").attr("value", "some value");

$("#txt").attr("value", "some value");

回答by Shomz

Chances are you're calling your jQuery code before the HTML input part. You can either place the jQuery stuff below it, or if you don't want to, you can do something like this:

您可能会在 HTML 输入部分之前调用 jQuery 代码。你可以将 jQuery 的东西放在它下面,或者如果你不想,你可以做这样的事情:

$(document).ready(function(){
    $("#txt").attr("value", "some value");
});

That will only run when the page is fully loaded.

这只会在页面完全加载时运行。

However, it's unclear if you're using AJAXto load those inputs into your DOM. If so, you need to call $("#txt").attr("value", "some value");in the onSuccess callback function which is fired after the AJAX successfully responds.

但是,不清楚您是否使用AJAX将这些输入加载到 DOM 中。如果是这样,您需要调用$("#txt").attr("value", "some value");在 AJAX 成功响应后触发的 onSuccess 回调函数。

回答by Rahul Tripathi

You can try something like this:-

你可以尝试这样的事情:-

  <input name="example" type="text" id="example" 
  size="50" value="MyDefaultText" onfocus="if(this.value=='MyDefaultText')this.value=''"
  onblur="if(this.value=='')this.value='MyDefaultText'" />

回答by chiliNUT

Excellent question. You would think clone would do this on its own, alas, it doesn't.

很好的问题。你会认为克隆会自己做这件事,唉,它没有。

Here is a sample than you can hopefully adapt to do what you need

这是一个示例,您希望能够适应您的需求

HTML

HTML

<div id=divToCopy>
    <input name=i1 value=foo><br>
    <input name=i2 value=bar>
</div>
<input type=button onclick=copyDiv(); value='Copy the div'>

<div id=newDiv>
    the copy will go here
</div>

JavaScript

JavaScript

    function copyDiv() {
        $('#newDiv').html($('#divToCopy').clone());

        $('#divToCopy :input').each(function() {
            var child=0;
            for (var i = 0; i < this.attributes.length; i++) {
                var attrib = this.attributes[i];
                var prop=$(this).prop(attrib.name);
                $($('#newDiv').find(' :input')[child]).prop(attrib.name,prop);
                child++;
            }
        });
    }

回答by Majid Fouladpour

But it does work: http://jsbin.com/eXEROtU/1/edit

但它确实有效:http: //jsbin.com/eXEROtU/1/edit

var html = '<input type="text" id="txt" value=""/>';

$(document).ready(function() {

  $("#load").click(function() {
    $("#sub_input_box").html(html);
  });

  $("#inspect").click(function() {
    alert($("#txt").val());
  });

});

回答by rocky

$(document).on('focusout','input[type="text"]',function(a){
console.log(a.target.value);
a.target.setAttribute("value",a.target.value);
});

this is the solution i found, i had to set the value attribute explicitly on loose focus from the text field

这是我找到的解决方案,我必须在文本字段的松散焦点上显式设置 value 属性

回答by William Yang

Have you tried:

你有没有尝试过:

$("#txt").val("Hello World!");

For settingthe text value, and,

用于设置文本值,以及,

var my_string = $("#txt").val();

For gettingthe text value.

用于获取文本值。

Let me know if it works.

让我知道它是否有效。