最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I pass value from javascript to html? - Stack Overflow

programmeradmin1浏览0评论

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 ??

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 ??

Share Improve this question asked Jul 27, 2012 at 9:28 crazyoxygencrazyoxygen 7161 gold badge11 silver badges31 bronze badges 1
  • 1 stackoverflow./…, 3829 results – KooiInc Commented Jul 27, 2012 at 9:32
Add a ment  | 

2 Answers 2

Reset to default 6

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

发布评论

评论列表(0)

  1. 暂无评论