react.js用javascript改变输入值 [英] react.js change input value with javascript

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

问题描述

我想用javascript / jquery更改值,但是当我点击提交时,会使用旧的初始值,而不是我的新值。

I would like to change the values with javascript/jquery but when I hit submit, the old initial values are used and not my new ones.

我无法更改反应代码,我只能用javascript / jquery更改值,如下所示。

I cannot change the react code, I can only change the values with javascript/jquery as shown below.

var value = "randomIntValue";
$("input[data-reactid='.0.2.0.0.0.0.0.2.0.0.3.0.3.0.0']").val(value).change();

以上代码不起作用!

推荐答案

从上面的评论我可以收集到的,你遇到的问题是你试图通过jQuery更改一个React控制的组件。

From what I can gather from the comments above, the problem you're having is that you're trying to change a React-controlled component through jQuery.

这是不可能的,因为您无法直接从视图更新React状态。当您运行jQuery代码时,更改仅暂时应用于DOM,但React不知道这一点。因此,当React需要重新渲染视图时,DOM中的旧值将被覆盖。

This is not possible because you cannot update the React state from the view directly. When you run your jQuery code, the changes are only temporarily applied to the DOM, but React does not know about this. As such, when React needs to re-render the view the old values in the DOM will be overwritten.

此外,通过访问组件data-reactid 是一种糟糕的方法,因为如果对React virtual-DOM进行任何更改,该值可能会发生变化。

Also, accessing the component via the data-reactid is a bad approach because the value is likely to change if any changes are made the React virtual-DOM.

使用React和尝试应用DOM更改时,jQuery通常是个坏主意。如果您无法访问/更改任何React代码,我担心您运气不好。

Using React and jQuery is usually a bad idea when trying to apply DOM changes. If you cannot access/change any React code, I'm afraid you're out of luck.

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

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