简体   繁体   中英

How can I pass value from javascript to html?

I have javascript function that get JSON from another php file but I can't pass value to html file. How can I do it ??

here plese take a look

<body>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
    $.getJSON("getquestion.php",function(result)
    {
        document.getElementById("question").innerHTML = result.test_question;   
        testform.textquestion.value = result.test_question;

    });
    </script>
<form name="testform"> 
<div id="question"></div>
<input id="aaa" type="text" name="textquestion"></input>

</body>

div:question can show data from result.test_question but input:aaa can't.

Could you please teach me how can I pass value to or ? and Can I assign name of function .getJSON and how ??

Use jQuery properly since you're already using jQuery anyways.

$("#question").html(result.test_question);
$('#aaa').val(result.test_question);

Change

testform.textquestion.value = result.test_question;

to

document.getElementById('aaa').value = result.test_question;

or

document.testform.textquestion.value = result.test_question;

Docs for accessing forms using JavaScript here

or as you have linked jQuery .. you could replace

document.getElementById("question").innerHTML = result.test_question;   
testform.textquestion.value = result.test_question;

with

$("#question").html(result.test_question);   
$('#aaa').val(result.test_question);

Docs for .html() here and .val() here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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