简体   繁体   English

我如何设置值以在jQuery中以编程方式输入文本框

[英]how i can set value to input text box programmatically in jquery

i set address image file to a text box with click a button this work okay. 我将地址图像文件设置为文本框,然后单击一个按钮,这样就可以了。

i need a listener to get value from text box and show alert. 我需要一个侦听器来从文本框中获取值并显示警报。 i don't need use change event i don't use keyboard value insert by a function 我不需要使用更改事件,我也不需要通过功能使用键盘值插入

$("#add").click(function(){
   $("input").val('http://localhost/afa/uploads/source/error-img.png'); 
});

after insert address file i show a alert with content value input 插入地址文件后,我显示带有内容值输入的警报

var val = $("#fieldID4").val();
files2.push({src:val});
updateList2(files2.length-1);


  function updateList2(n) {
    var e = thumb.clone();
    e.find('img').attr('src',files2[n].src);
    e.find('button').click(removeFromList).data('n',n);
    gallery.append(e);

    function removeFromList() {
        files2[$(this).data('n')] = null;
        $(this).parent().remove();
    }    
}

i solve this problem with use onchange event 我通过使用onchange事件解决了这个问题

[ http://www.w3schools.com/jsref/event_onchange.asp][1] [ http://www.w3schools.com/jsref/event_onchange.asp][1]

   <input  id="fieldID4" type="text" onchange="myFunction()" value="" >

function myFunction() {
    var val = document.getElementById("fieldID4").value;

files2.push({src:val});
updateList2(files2.length-1);


}

As well described in fire event on programmatic change you may do something like: 程序更改中的火灾事件所述,您可以执行以下操作:

 window.onload = function (e) { var myInput = document.getElementById('myInput'); Object.defineProperty(myInput, 'value', { enumerable: true, configurable: true, get: function(){ return this.getAttribute('value'); }, set: function(val){ this.setAttribute('value', val); var event = new Event('InputChanged'); this.dispatchEvent(event); } }); } $(function () { $('#myInput').on('InputChanged', function(e) { alert('InputChanged Event: ' + this.value); }); $('#myInput').on('change', function(e) { alert('Standard input change event: ' + this.value); }); $('#btn').on('click', function(e) { e.preventDefault(); var newValue = $('#newInput').val() || 'NewText'; $('#myInput').val(newValue); }) }); 
 <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <form> Original Input: <input id="myInput"><br> Write here....: <input id="newInput"><br> <button id="btn">Change Input Text</button> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM