我有这个网页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Submit Form</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
let amount = 300;
$("#submitBtn").click(function(){
alert(amount)
$.post("https://example.com/posted.php",
{
price: amount,
city: "Duckburg"
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<form action="#" method="post" id="myForm">
<label>First Name:</label>
<input type="text" name="first-name">
<button type="button" id="submitBtn">Submit Form</button>
</form>
</body>
</html>
我已经用webview加载了
import { StyleSheet, Text, View, Button } from 'react-native';
import React, { useRef, useState } from 'react';
import { useNavigation } from '@react-navigation/native'
import { WebView } from 'react-native-webview';
const Home = () => {
const navigation = useNavigation()
const webViewRef = useRef(null);
const [webviewLoaded, setWebviewLoaded] = useState(false);
const modifyFooVariable = () => {
const jsCode = "window.amount = '6855555';";
webViewRef.current.injectJavaScript(jsCode);
};
return (
<WebView
ref={webViewRef}
source={{ uri: 'https://example.com/test.html' }}
javaScriptEnabled={true}
domStorageEnabled={true}
onLoad={() => setWebviewLoaded(true)}
onMessage={(event) => {
if (event.nativeEvent.data === 'modifyFooVariable') {
modifyFooVariable();
}
}}
injectedJavaScript={`
document.addEventListener('DOMContentLoaded', function() {
window.ReactNativeWebView.postMessage('webviewLoaded');
});
`}
/>
)
}
export default Home
const styles = StyleSheet.create({
WebViewStyle: {
justifyContent: 'center',
alignItems: 'center',
flex: 0.5,
marginTop: 30,
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
ct:{
color:'red',
}
});
是否可以在我正在加载的远程文件中更改此变量
let amount = 300;
从我的react本地网络视图?
1条答案
按热度按时间o2g1uqev1#
在我的react原生代码中,我添加了
在我的html代码中
我能让它工作。