简体   繁体   English

以编程方式更改javascript的元素值

[英]programmatically change element value javascript

I wanting to get the currentTarget to pass to a function, once I have changed the value of a select programmatically. 一旦我以编程方式更改了select的值,我想让currentTarget传递给函数。

Is this possible? 这可能吗? Something like, 就像是,

$('input[name="client_id"]').val(model.get('id')) //Change the value of select
this.doSave( $('input[name="client_id"]')); //Do save expects event(e) as parameter

function doSave(event){
    console.log($(event.currentTarget);
}

I have simplified this down massively, basically I am changing a select programmatically, and then want to fire my save event, which normally gets triggered on a change event, and as a result takes an event as a parameter, but all I have is the DOM element? 我已经将其简化了很多,基本上我正在以编程方式更改选择,然后要触发我的保存事件,该事件通常在更改事件上触发,因此将事件作为参数,但是我所拥有的只是DOM元素? Is there a way to send it an event? 有没有办法发送事件?

Use the change event handler as below : 使用更改事件处理程序,如下所示:

$('input[name="client_id"]').on('change', function(event) {
   console.log($(event.currentTarget);
});

or 要么

$('input[name="client_id"]').on('change', doSave);

function doSave(event){
    console.log($(event.currentTarget);
}

First of all, This 首先,这个

$('input[name="client_id"]')

apparently, changes the value of an input and not select. 显然,更改输入的值而不是选择。

This 这个

$('select[name="client_id"]').val(model.get('id'))

changes the selected option in a select. 更改选择中的所选选项。

You can bind the change event on a select like 您可以将更改事件绑定到类似的选择上

$('select').change(function(event){
    //use event here 
    //or pass to doSave(event)
});

and it will be fired when call 它将在呼叫时被触发

$('input[name="client_id"]').change();

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

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