React.js设置输入值 [英] React.js setting value of input

查看:69
本文介绍了React.js设置输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是react.js的初学者,这很神奇,但我面临一个真正的问题:我需要使用普通和纯javascript设置输入的值,但形成某种原因react.js没有按预期工作。举个例子:打开这个URL http://autoescolalatorreta.wix.com/latorreta#!contact/c24vq

I am a beginner with react.js and it's amazing but I am facing a real problem: I need to set the value of an input using plain and pure javascript but form some reason react.js is not working as expected. Take this example: open this URL http://autoescolalatorreta.wix.com/latorreta#!contact/c24vq

您将看到有一个电子邮件输入字段,其ID为CntctFrm2emailField。我尝试使用此代码使用javascript更改它的值:

You will see there is an "Email" input field which id is "CntctFrm2emailField". I try to change the value of it using javascript with this code:

document.getElementById("CntctFrm2emailField").value = "testing@gmail.com";

但由于某些原因,如果你尝试发送,React.js不会在其核心原因上更新此值表单(单击SEND按钮)您将看到它将显示一条错误消息,指出电子邮件未正确填写。但是,一旦我在电子邮件字段中单击并键入任何字母并单击SEND按钮就可以正常工作,我的意思是,React.js会看到新值。

But for some reason React.js is not updating this value on its core cause if you try to send the form (Clicking the SEND button) you will see that it will display an error message saying that email is not filled right. BUT once I click inside the email field and type any letter and click the SEND button it works fine, I mean, React.js sees the new value.

那怎么能我更改了一个输入值并让React.js也更新了这个值?

So how can I change an input value and have React.js to update that value too?

推荐答案

我已经编写并使用此函数来触发该字段的更改状态:

I have written and use this function to trigger the change state for the field:

function doEvent( obj, event ) {
    /* Created by David@Refoua.me */
    var event = new Event( event, {target: obj, bubbles: true} );
    return obj ? obj.dispatchEvent(event) : false;
}

像这样使用:

var el = document.getElementById("CntctFrm2emailField");
el.value = "testing@gmail.com";
doEvent( el, 'input' );

这篇关于React.js设置输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆