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

JavaScript in HTML: Getting undefined input value? - Stack Overflow

programmeradmin5浏览0评论

For some reason, when I enter a value in the following script, the value of ISBN is always undefined. I'm not sure why this is the case. I have set the button to run the function onclick as well...

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";  

    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);
}

Is there anything I'm missing about how to get input values?

For some reason, when I enter a value in the following script, the value of ISBN is always undefined. I'm not sure why this is the case. I have set the button to run the function onclick as well...

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.";  

    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);
}

Is there anything I'm missing about how to get input values?

Share Improve this question edited Jun 13, 2019 at 13:31 Loveen Dyall 8342 gold badges8 silver badges21 bronze badges asked Feb 1, 2017 at 20:21 QuintyQuinty 1991 gold badge1 silver badge12 bronze badges 1
  • 1 submit() method doesn't have return value, that's why you're getting undefined – Jovica Šuša Commented Feb 1, 2017 at 20:26
Add a ment  | 

2 Answers 2

Reset to default 6
var isbn = document.getElementById('someIdYouAssignTheInput').value;

As mentioned above, submit doesn't return anything, which is why the value is undefined. You want to get the value of the input element. The best way to do that is to assign it an id, then use the code I included above.

Good luck!

try

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

to retrieve the input text box's value.

发布评论

评论列表(0)

  1. 暂无评论