0

所以我为此工作了好几个小时,但我无法找到正确的算法,当文本区域的值发生变化时如何更新函数的参数。

<script>
    details = "";
    postid = <? php echo $_GET['id']; ?> ;
    userid = <? php echo User::GetUserID($_SESSION['username']); ?> ;
    postedby = posted_by;

    function offerIt() {
        //created a function so that we can get the latest value of the textarea because at first it was giving the default value which was null because at the page load the value is null of the textarea
        addPostOffer('' + details + ',' + postid + ',' + userid + ',' + postedby + '');
    }
</script>
<textarea placeholder="Type in you offer details" rows="5" class="input-block-level" id="offer_details" onblur="details=this.value"></textarea>
<script>
    document.write("<input type='submit' class='btn btn-primary pull-right' onclick='offerIt()' value='Offer It' />");
</script>

所以实际上我希望details变量得到更新。首先我将它的值设置为 "" 然后onblur是 textarea( #offer_details) 的值,但现在我希望 addPostOffer 的参数也被更新,但这并没有发生!我怎样才能做到这一点?

这就是document.write正在写的内容:

<input type="submit" class="btn btn-primary pull-right" onclick="offerIt()" value="Offer It">

请帮忙。

4

1 回答 1

0

我测试了你的代码,问题出在这里:

postid = <? php echo $_GET['id']; ?> ;
userid = <? php echo User::GetUserID($_SESSION['username']); ?> ;

应改为:

postid = "<?php echo $_GET['id']; ?>";
userid = "<?php echo User::GetUserID($_SESSION['username']); ?>";

整个代码现在变成了,我添加了posted_by = userid; 定义它:

<script>
    details = "";
    postid = "<?php echo $_GET['id']; ?>";
    userid = "<?php echo User::GetUserID($_SESSION['username']); ?>";
  posted_by = userid;
    postedby = posted_by;

    function offerIt() {
        //created a function so that we can get the latest value of the textarea because at first it was giving the default value which was null because at the page load the value is null of the textarea
        addPostOffer('' + details + ',' + postid + ',' + userid + ',' + postedby + '');
    }
</script>
<textarea placeholder="Type in you offer details" rows="5" class="input-block-level" id="offer_details" onblur="details=this.value"></textarea>
<script>
    document.write("<input type='submit' class='btn btn-primary pull-right' onclick='offerIt()' value='Offer It' />");
</script>
于 2013-10-20T19:56:57.180 回答