【问题标题】:Inserting text into iframe form field & submit将文本插入 iframe 表单字段并提交
【发布时间】:2013-10-05 10:25:27
【问题描述】:

好的,所以我们对作为 iframe 嵌入的配置文件有一个星级评分系统,因为它暴露的数据库表等是不安全的,但这即将到来。现在我们想将评级值插入到该 iframe 内 1-5 的表单中的隐藏字段中,并自动提交。我们怎样才能做到这一点?几个在线示例不起作用。

在 iframe 内:

<form action="submit_rating.php" method="POST">
  <input type="text" name="1" id="rate1" maxlength="1">
  <input type="text" name="2" id="rate2" maxlength="1">
  <input type="text" name="3" id="rate3" maxlength="1">
  <input type="text" name="4" id="rate4" maxlength="1">
  <input type="text" name="5" id="rate5" maxlength="1">
  <input type="submit" value="Rate" style="display:none;" />
</form>

iframe:

<iframe src="http://domain.com/submit_rating.php" frameborder="0" scrolling="no" height="10" width="25" name="frame"></iframe>

评分链接:

<a href="howtoinsert?">1</a>
<a href="howtoinsert?">2</a>
<a href="howtoinsert?">3</a>
<a href="howtoinsert?">4</a>
<a href="howtoinsert?">5</a>

谢谢。就像我说的那样,所有其他示例都是垃圾并且不起作用。

【问题讨论】:

    标签: javascript forms iframe insert submit


    【解决方案1】:

    你可以这样做(使用 jquery):

    评级网站:

    <!DOCTYPE html>
    <html>
      <head>
        <title></title>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script>
          function setRating(rating) {
            var frame = $('iframe')[0].contentWindow.document;
            $(frame).find('#rate'+rating).val('1');
            $(frame).find('form').submit();
          }
        </script>
      </head>
      <body>
        <iframe src="frame.html" frameborder="0" scrolling="no" height="10" width="25" name="frame"></iframe>
        <a href="javascript:setRating(1)">1</a>
        <a href="javascript:setRating(2)">2</a>
        <a href="javascript:setRating(3)">3</a>
        <a href="javascript:setRating(4)">4</a>
        <a href="javascript:setRating(5)">5</a>
      </body>
    </html>
    

    框架内容:

    <!DOCTYPE html>
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <form action="submit_rating.php" method="POST">
          <input type="text" name="1" id="rate1" maxlength="1">
          <input type="text" name="2" id="rate2" maxlength="1">
          <input type="text" name="3" id="rate3" maxlength="1">
          <input type="text" name="4" id="rate4" maxlength="1">
          <input type="text" name="5" id="rate5" maxlength="1">
          <input type="submit" value="Rate" style="display:none;" />
        </form>
      </body>
    </html>
    

    但更好的方法是使用 ajax:

    <!DOCTYPE html>
    <html>
      <head>
        <title></title>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script>
          function setRating(rating) {
            $.post('submit_rating.php', {
              rating: rating
            });
          }
        </script>
      </head>
      <body>
        <a href="javascript:setRating(1)">1</a>
        <a href="javascript:setRating(2)">2</a>
        <a href="javascript:setRating(3)">3</a>
        <a href="javascript:setRating(4)">4</a>
        <a href="javascript:setRating(5)">5</a>
      </body>
    </html>
    

    【讨论】:

    • 现在如果我只想插入一个自定义值,该怎么做? $(frame).find('#about_me'+rating).val('1');通过示例自定义输入名称。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多