2013-10-05 28 views
1

好吧,我們對嵌入爲iframe的配置文件具有星級評分系統,因爲它的暴露數據庫表等不安全,但即將到來。現在,我們要將評分值從1-5的iframe中的表單中的隱藏字段插入,並自動提交。我們怎樣才能做到這一點?幾個在線示例不起作用。將文本插入到iframe表單域並提交

裏面的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> 

謝謝。就像我說的,所有其他例子都是垃圾,並沒有工作。

回答

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> 
+0

現在,如果我只想插入一個自定義值, 這個怎麼做? $(幀).find( '#about_me' +等級).VAL( '1');通過示例自定義輸入名稱。謝謝 –