簡體   English   中英

HTML中的JavaScript:獲取未定義的輸入值?

[英]JavaScript in HTML: Getting undefined input value?

由於某些原因,當我在以下腳本中輸入值時,ISBN的值始終未定義。 我不確定為什么會這樣。 我還設置了按鈕以運行onclick功能...

HTML:

<form id="frm1">
    Book ISBN: <input type="text" name="fname"><br><br>
    <input type="button" onclick="myFunction()" value=行きましょう!>
</form>

的JavaScript

function myFunction() {
    var isbn = document.getElementById("frm1").submit(); // Replace with ISBN / EISBN / ASIN of the book
    alert("Starting!");
    alert(isbn);

    var api_key = "cf6f5fe91531a855495d77fc06dd9ada6f1fa1f7"; //Replace with the developer key (Submit the form to get a key)

    //Optional Settings
    var smiley = 'true'; // Change to false if do not want to show smiley with each review
    var width = 700;     // Width of the reviews iframe
    var height = 400;    // Height of the reviews iframe
    var wf = document.createElement('script');
    var host = "idreambooks.com";  

    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
              '://' + host + '/reviews_widget.js?api_key=' + api_key + '&isbn=' + isbn +
              '&smiley=' + smiley + '&width=' + width + '&height=' + height;
    wf.type = 'text/javascript';
    wf.async = 'true';

    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
}

關於獲取輸入值,我缺少什么嗎?

var isbn = document.getElementById('someIdYouAssignTheInput').value;

如上所述,submitt不返回任何東西,這就是為什么該值未定義的原因。 您想要獲取輸入元素的 最好的方法是給它分配一個ID,然后使用上面包含的代碼。

祝好運!

嘗試

 var isbn = document.getElementsByName("fname")[0].value;

檢索輸入文本框的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM